PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/sound/usb/midi.c

https://github.com/mstsirkin/kvm
C | 1783 lines | 1398 code | 166 blank | 219 comment | 239 complexity | 3daa0a56f503b1c171a3eaa9129b4fdf MD5 | raw file
  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,
  9. * the "USB Device Class Definition for MIDI Devices" by Roland
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions, and the following disclaimer,
  16. * without modification.
  17. * 2. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed and/or modified under the
  21. * terms of the GNU General Public License as published by the Free Software
  22. * Foundation; either version 2 of the License, or (at your option) any later
  23. * version.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  29. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #include <linux/kernel.h>
  38. #include <linux/types.h>
  39. #include <linux/bitops.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/string.h>
  43. #include <linux/init.h>
  44. #include <linux/slab.h>
  45. #include <linux/timer.h>
  46. #include <linux/usb.h>
  47. #include <linux/wait.h>
  48. #include <linux/usb/audio.h>
  49. #include <sound/core.h>
  50. #include <sound/control.h>
  51. #include <sound/rawmidi.h>
  52. #include <sound/asequencer.h>
  53. #include "usbaudio.h"
  54. #include "midi.h"
  55. #include "power.h"
  56. #include "helper.h"
  57. /*
  58. * define this to log all USB packets
  59. */
  60. /* #define DUMP_PACKETS */
  61. /*
  62. * how long to wait after some USB errors, so that khubd can disconnect() us
  63. * without too many spurious errors
  64. */
  65. #define ERROR_DELAY_JIFFIES (HZ / 10)
  66. #define OUTPUT_URBS 7
  67. #define INPUT_URBS 7
  68. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  69. MODULE_DESCRIPTION("USB Audio/MIDI helper module");
  70. MODULE_LICENSE("Dual BSD/GPL");
  71. struct usb_ms_header_descriptor {
  72. __u8 bLength;
  73. __u8 bDescriptorType;
  74. __u8 bDescriptorSubtype;
  75. __u8 bcdMSC[2];
  76. __le16 wTotalLength;
  77. } __attribute__ ((packed));
  78. struct usb_ms_endpoint_descriptor {
  79. __u8 bLength;
  80. __u8 bDescriptorType;
  81. __u8 bDescriptorSubtype;
  82. __u8 bNumEmbMIDIJack;
  83. __u8 baAssocJackID[0];
  84. } __attribute__ ((packed));
  85. struct snd_usb_midi_in_endpoint;
  86. struct snd_usb_midi_out_endpoint;
  87. struct snd_usb_midi_endpoint;
  88. struct usb_protocol_ops {
  89. void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
  90. void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
  91. void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
  92. void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
  93. void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
  94. };
  95. struct snd_usb_midi {
  96. struct usb_device *dev;
  97. struct snd_card *card;
  98. struct usb_interface *iface;
  99. const struct snd_usb_audio_quirk *quirk;
  100. struct snd_rawmidi *rmidi;
  101. struct usb_protocol_ops* usb_protocol_ops;
  102. struct list_head list;
  103. struct timer_list error_timer;
  104. spinlock_t disc_lock;
  105. struct mutex mutex;
  106. u32 usb_id;
  107. int next_midi_device;
  108. struct snd_usb_midi_endpoint {
  109. struct snd_usb_midi_out_endpoint *out;
  110. struct snd_usb_midi_in_endpoint *in;
  111. } endpoints[MIDI_MAX_ENDPOINTS];
  112. unsigned long input_triggered;
  113. unsigned int opened;
  114. unsigned char disconnected;
  115. struct snd_kcontrol *roland_load_ctl;
  116. };
  117. struct snd_usb_midi_out_endpoint {
  118. struct snd_usb_midi* umidi;
  119. struct out_urb_context {
  120. struct urb *urb;
  121. struct snd_usb_midi_out_endpoint *ep;
  122. } urbs[OUTPUT_URBS];
  123. unsigned int active_urbs;
  124. unsigned int drain_urbs;
  125. int max_transfer; /* size of urb buffer */
  126. struct tasklet_struct tasklet;
  127. unsigned int next_urb;
  128. spinlock_t buffer_lock;
  129. struct usbmidi_out_port {
  130. struct snd_usb_midi_out_endpoint* ep;
  131. struct snd_rawmidi_substream *substream;
  132. int active;
  133. uint8_t cable; /* cable number << 4 */
  134. uint8_t state;
  135. #define STATE_UNKNOWN 0
  136. #define STATE_1PARAM 1
  137. #define STATE_2PARAM_1 2
  138. #define STATE_2PARAM_2 3
  139. #define STATE_SYSEX_0 4
  140. #define STATE_SYSEX_1 5
  141. #define STATE_SYSEX_2 6
  142. uint8_t data[2];
  143. } ports[0x10];
  144. int current_port;
  145. wait_queue_head_t drain_wait;
  146. };
  147. struct snd_usb_midi_in_endpoint {
  148. struct snd_usb_midi* umidi;
  149. struct urb* urbs[INPUT_URBS];
  150. struct usbmidi_in_port {
  151. struct snd_rawmidi_substream *substream;
  152. u8 running_status_length;
  153. } ports[0x10];
  154. u8 seen_f5;
  155. u8 error_resubmit;
  156. int current_port;
  157. };
  158. static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
  159. static const uint8_t snd_usbmidi_cin_length[] = {
  160. 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
  161. };
  162. /*
  163. * Submits the URB, with error handling.
  164. */
  165. static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
  166. {
  167. int err = usb_submit_urb(urb, flags);
  168. if (err < 0 && err != -ENODEV)
  169. snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
  170. return err;
  171. }
  172. /*
  173. * Error handling for URB completion functions.
  174. */
  175. static int snd_usbmidi_urb_error(int status)
  176. {
  177. switch (status) {
  178. /* manually unlinked, or device gone */
  179. case -ENOENT:
  180. case -ECONNRESET:
  181. case -ESHUTDOWN:
  182. case -ENODEV:
  183. return -ENODEV;
  184. /* errors that might occur during unplugging */
  185. case -EPROTO:
  186. case -ETIME:
  187. case -EILSEQ:
  188. return -EIO;
  189. default:
  190. snd_printk(KERN_ERR "urb status %d\n", status);
  191. return 0; /* continue */
  192. }
  193. }
  194. /*
  195. * Receives a chunk of MIDI data.
  196. */
  197. static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
  198. uint8_t* data, int length)
  199. {
  200. struct usbmidi_in_port* port = &ep->ports[portidx];
  201. if (!port->substream) {
  202. snd_printd("unexpected port %d!\n", portidx);
  203. return;
  204. }
  205. if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
  206. return;
  207. snd_rawmidi_receive(port->substream, data, length);
  208. }
  209. #ifdef DUMP_PACKETS
  210. static void dump_urb(const char *type, const u8 *data, int length)
  211. {
  212. snd_printk(KERN_DEBUG "%s packet: [", type);
  213. for (; length > 0; ++data, --length)
  214. printk(" %02x", *data);
  215. printk(" ]\n");
  216. }
  217. #else
  218. #define dump_urb(type, data, length) /* nothing */
  219. #endif
  220. /*
  221. * Processes the data read from the device.
  222. */
  223. static void snd_usbmidi_in_urb_complete(struct urb* urb)
  224. {
  225. struct snd_usb_midi_in_endpoint* ep = urb->context;
  226. if (urb->status == 0) {
  227. dump_urb("received", urb->transfer_buffer, urb->actual_length);
  228. ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
  229. urb->actual_length);
  230. } else {
  231. int err = snd_usbmidi_urb_error(urb->status);
  232. if (err < 0) {
  233. if (err != -ENODEV) {
  234. ep->error_resubmit = 1;
  235. mod_timer(&ep->umidi->error_timer,
  236. jiffies + ERROR_DELAY_JIFFIES);
  237. }
  238. return;
  239. }
  240. }
  241. urb->dev = ep->umidi->dev;
  242. snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
  243. }
  244. static void snd_usbmidi_out_urb_complete(struct urb* urb)
  245. {
  246. struct out_urb_context *context = urb->context;
  247. struct snd_usb_midi_out_endpoint* ep = context->ep;
  248. unsigned int urb_index;
  249. spin_lock(&ep->buffer_lock);
  250. urb_index = context - ep->urbs;
  251. ep->active_urbs &= ~(1 << urb_index);
  252. if (unlikely(ep->drain_urbs)) {
  253. ep->drain_urbs &= ~(1 << urb_index);
  254. wake_up(&ep->drain_wait);
  255. }
  256. spin_unlock(&ep->buffer_lock);
  257. if (urb->status < 0) {
  258. int err = snd_usbmidi_urb_error(urb->status);
  259. if (err < 0) {
  260. if (err != -ENODEV)
  261. mod_timer(&ep->umidi->error_timer,
  262. jiffies + ERROR_DELAY_JIFFIES);
  263. return;
  264. }
  265. }
  266. snd_usbmidi_do_output(ep);
  267. }
  268. /*
  269. * This is called when some data should be transferred to the device
  270. * (from one or more substreams).
  271. */
  272. static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
  273. {
  274. unsigned int urb_index;
  275. struct urb* urb;
  276. unsigned long flags;
  277. spin_lock_irqsave(&ep->buffer_lock, flags);
  278. if (ep->umidi->disconnected) {
  279. spin_unlock_irqrestore(&ep->buffer_lock, flags);
  280. return;
  281. }
  282. urb_index = ep->next_urb;
  283. for (;;) {
  284. if (!(ep->active_urbs & (1 << urb_index))) {
  285. urb = ep->urbs[urb_index].urb;
  286. urb->transfer_buffer_length = 0;
  287. ep->umidi->usb_protocol_ops->output(ep, urb);
  288. if (urb->transfer_buffer_length == 0)
  289. break;
  290. dump_urb("sending", urb->transfer_buffer,
  291. urb->transfer_buffer_length);
  292. urb->dev = ep->umidi->dev;
  293. if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
  294. break;
  295. ep->active_urbs |= 1 << urb_index;
  296. }
  297. if (++urb_index >= OUTPUT_URBS)
  298. urb_index = 0;
  299. if (urb_index == ep->next_urb)
  300. break;
  301. }
  302. ep->next_urb = urb_index;
  303. spin_unlock_irqrestore(&ep->buffer_lock, flags);
  304. }
  305. static void snd_usbmidi_out_tasklet(unsigned long data)
  306. {
  307. struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
  308. snd_usbmidi_do_output(ep);
  309. }
  310. /* called after transfers had been interrupted due to some USB error */
  311. static void snd_usbmidi_error_timer(unsigned long data)
  312. {
  313. struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
  314. unsigned int i, j;
  315. spin_lock(&umidi->disc_lock);
  316. if (umidi->disconnected) {
  317. spin_unlock(&umidi->disc_lock);
  318. return;
  319. }
  320. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  321. struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
  322. if (in && in->error_resubmit) {
  323. in->error_resubmit = 0;
  324. for (j = 0; j < INPUT_URBS; ++j) {
  325. in->urbs[j]->dev = umidi->dev;
  326. snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
  327. }
  328. }
  329. if (umidi->endpoints[i].out)
  330. snd_usbmidi_do_output(umidi->endpoints[i].out);
  331. }
  332. spin_unlock(&umidi->disc_lock);
  333. }
  334. /* helper function to send static data that may not DMA-able */
  335. static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
  336. const void *data, int len)
  337. {
  338. int err = 0;
  339. void *buf = kmemdup(data, len, GFP_KERNEL);
  340. if (!buf)
  341. return -ENOMEM;
  342. dump_urb("sending", buf, len);
  343. if (ep->urbs[0].urb)
  344. err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
  345. buf, len, NULL, 250);
  346. kfree(buf);
  347. return err;
  348. }
  349. /*
  350. * Standard USB MIDI protocol: see the spec.
  351. * Midiman protocol: like the standard protocol, but the control byte is the
  352. * fourth byte in each packet, and uses length instead of CIN.
  353. */
  354. static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
  355. uint8_t* buffer, int buffer_length)
  356. {
  357. int i;
  358. for (i = 0; i + 3 < buffer_length; i += 4)
  359. if (buffer[i] != 0) {
  360. int cable = buffer[i] >> 4;
  361. int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
  362. snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
  363. }
  364. }
  365. static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
  366. uint8_t* buffer, int buffer_length)
  367. {
  368. int i;
  369. for (i = 0; i + 3 < buffer_length; i += 4)
  370. if (buffer[i + 3] != 0) {
  371. int port = buffer[i + 3] >> 4;
  372. int length = buffer[i + 3] & 3;
  373. snd_usbmidi_input_data(ep, port, &buffer[i], length);
  374. }
  375. }
  376. /*
  377. * Buggy M-Audio device: running status on input results in a packet that has
  378. * the data bytes but not the status byte and that is marked with CIN 4.
  379. */
  380. static void snd_usbmidi_maudio_broken_running_status_input(
  381. struct snd_usb_midi_in_endpoint* ep,
  382. uint8_t* buffer, int buffer_length)
  383. {
  384. int i;
  385. for (i = 0; i + 3 < buffer_length; i += 4)
  386. if (buffer[i] != 0) {
  387. int cable = buffer[i] >> 4;
  388. u8 cin = buffer[i] & 0x0f;
  389. struct usbmidi_in_port *port = &ep->ports[cable];
  390. int length;
  391. length = snd_usbmidi_cin_length[cin];
  392. if (cin == 0xf && buffer[i + 1] >= 0xf8)
  393. ; /* realtime msg: no running status change */
  394. else if (cin >= 0x8 && cin <= 0xe)
  395. /* channel msg */
  396. port->running_status_length = length - 1;
  397. else if (cin == 0x4 &&
  398. port->running_status_length != 0 &&
  399. buffer[i + 1] < 0x80)
  400. /* CIN 4 that is not a SysEx */
  401. length = port->running_status_length;
  402. else
  403. /*
  404. * All other msgs cannot begin running status.
  405. * (A channel msg sent as two or three CIN 0xF
  406. * packets could in theory, but this device
  407. * doesn't use this format.)
  408. */
  409. port->running_status_length = 0;
  410. snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
  411. }
  412. }
  413. /*
  414. * CME protocol: like the standard protocol, but SysEx commands are sent as a
  415. * single USB packet preceded by a 0x0F byte.
  416. */
  417. static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
  418. uint8_t *buffer, int buffer_length)
  419. {
  420. if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
  421. snd_usbmidi_standard_input(ep, buffer, buffer_length);
  422. else
  423. snd_usbmidi_input_data(ep, buffer[0] >> 4,
  424. &buffer[1], buffer_length - 1);
  425. }
  426. /*
  427. * Adds one USB MIDI packet to the output buffer.
  428. */
  429. static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
  430. uint8_t p1, uint8_t p2, uint8_t p3)
  431. {
  432. uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
  433. buf[0] = p0;
  434. buf[1] = p1;
  435. buf[2] = p2;
  436. buf[3] = p3;
  437. urb->transfer_buffer_length += 4;
  438. }
  439. /*
  440. * Adds one Midiman packet to the output buffer.
  441. */
  442. static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
  443. uint8_t p1, uint8_t p2, uint8_t p3)
  444. {
  445. uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
  446. buf[0] = p1;
  447. buf[1] = p2;
  448. buf[2] = p3;
  449. buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
  450. urb->transfer_buffer_length += 4;
  451. }
  452. /*
  453. * Converts MIDI commands to USB MIDI packets.
  454. */
  455. static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
  456. uint8_t b, struct urb* urb)
  457. {
  458. uint8_t p0 = port->cable;
  459. void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
  460. port->ep->umidi->usb_protocol_ops->output_packet;
  461. if (b >= 0xf8) {
  462. output_packet(urb, p0 | 0x0f, b, 0, 0);
  463. } else if (b >= 0xf0) {
  464. switch (b) {
  465. case 0xf0:
  466. port->data[0] = b;
  467. port->state = STATE_SYSEX_1;
  468. break;
  469. case 0xf1:
  470. case 0xf3:
  471. port->data[0] = b;
  472. port->state = STATE_1PARAM;
  473. break;
  474. case 0xf2:
  475. port->data[0] = b;
  476. port->state = STATE_2PARAM_1;
  477. break;
  478. case 0xf4:
  479. case 0xf5:
  480. port->state = STATE_UNKNOWN;
  481. break;
  482. case 0xf6:
  483. output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
  484. port->state = STATE_UNKNOWN;
  485. break;
  486. case 0xf7:
  487. switch (port->state) {
  488. case STATE_SYSEX_0:
  489. output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
  490. break;
  491. case STATE_SYSEX_1:
  492. output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
  493. break;
  494. case STATE_SYSEX_2:
  495. output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
  496. break;
  497. }
  498. port->state = STATE_UNKNOWN;
  499. break;
  500. }
  501. } else if (b >= 0x80) {
  502. port->data[0] = b;
  503. if (b >= 0xc0 && b <= 0xdf)
  504. port->state = STATE_1PARAM;
  505. else
  506. port->state = STATE_2PARAM_1;
  507. } else { /* b < 0x80 */
  508. switch (port->state) {
  509. case STATE_1PARAM:
  510. if (port->data[0] < 0xf0) {
  511. p0 |= port->data[0] >> 4;
  512. } else {
  513. p0 |= 0x02;
  514. port->state = STATE_UNKNOWN;
  515. }
  516. output_packet(urb, p0, port->data[0], b, 0);
  517. break;
  518. case STATE_2PARAM_1:
  519. port->data[1] = b;
  520. port->state = STATE_2PARAM_2;
  521. break;
  522. case STATE_2PARAM_2:
  523. if (port->data[0] < 0xf0) {
  524. p0 |= port->data[0] >> 4;
  525. port->state = STATE_2PARAM_1;
  526. } else {
  527. p0 |= 0x03;
  528. port->state = STATE_UNKNOWN;
  529. }
  530. output_packet(urb, p0, port->data[0], port->data[1], b);
  531. break;
  532. case STATE_SYSEX_0:
  533. port->data[0] = b;
  534. port->state = STATE_SYSEX_1;
  535. break;
  536. case STATE_SYSEX_1:
  537. port->data[1] = b;
  538. port->state = STATE_SYSEX_2;
  539. break;
  540. case STATE_SYSEX_2:
  541. output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
  542. port->state = STATE_SYSEX_0;
  543. break;
  544. }
  545. }
  546. }
  547. static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
  548. struct urb *urb)
  549. {
  550. int p;
  551. /* FIXME: lower-numbered ports can starve higher-numbered ports */
  552. for (p = 0; p < 0x10; ++p) {
  553. struct usbmidi_out_port* port = &ep->ports[p];
  554. if (!port->active)
  555. continue;
  556. while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
  557. uint8_t b;
  558. if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
  559. port->active = 0;
  560. break;
  561. }
  562. snd_usbmidi_transmit_byte(port, b, urb);
  563. }
  564. }
  565. }
  566. static struct usb_protocol_ops snd_usbmidi_standard_ops = {
  567. .input = snd_usbmidi_standard_input,
  568. .output = snd_usbmidi_standard_output,
  569. .output_packet = snd_usbmidi_output_standard_packet,
  570. };
  571. static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
  572. .input = snd_usbmidi_midiman_input,
  573. .output = snd_usbmidi_standard_output,
  574. .output_packet = snd_usbmidi_output_midiman_packet,
  575. };
  576. static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
  577. .input = snd_usbmidi_maudio_broken_running_status_input,
  578. .output = snd_usbmidi_standard_output,
  579. .output_packet = snd_usbmidi_output_standard_packet,
  580. };
  581. static struct usb_protocol_ops snd_usbmidi_cme_ops = {
  582. .input = snd_usbmidi_cme_input,
  583. .output = snd_usbmidi_standard_output,
  584. .output_packet = snd_usbmidi_output_standard_packet,
  585. };
  586. /*
  587. * AKAI MPD16 protocol:
  588. *
  589. * For control port (endpoint 1):
  590. * ==============================
  591. * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
  592. * SysEx message (msg_len=9 bytes long).
  593. *
  594. * For data port (endpoint 2):
  595. * ===========================
  596. * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
  597. * MIDI message (msg_len bytes long)
  598. *
  599. * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
  600. */
  601. static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
  602. uint8_t *buffer, int buffer_length)
  603. {
  604. unsigned int pos = 0;
  605. unsigned int len = (unsigned int)buffer_length;
  606. while (pos < len) {
  607. unsigned int port = (buffer[pos] >> 4) - 1;
  608. unsigned int msg_len = buffer[pos] & 0x0f;
  609. pos++;
  610. if (pos + msg_len <= len && port < 2)
  611. snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
  612. pos += msg_len;
  613. }
  614. }
  615. #define MAX_AKAI_SYSEX_LEN 9
  616. static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
  617. struct urb *urb)
  618. {
  619. uint8_t *msg;
  620. int pos, end, count, buf_end;
  621. uint8_t tmp[MAX_AKAI_SYSEX_LEN];
  622. struct snd_rawmidi_substream *substream = ep->ports[0].substream;
  623. if (!ep->ports[0].active)
  624. return;
  625. msg = urb->transfer_buffer + urb->transfer_buffer_length;
  626. buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;
  627. /* only try adding more data when there's space for at least 1 SysEx */
  628. while (urb->transfer_buffer_length < buf_end) {
  629. count = snd_rawmidi_transmit_peek(substream,
  630. tmp, MAX_AKAI_SYSEX_LEN);
  631. if (!count) {
  632. ep->ports[0].active = 0;
  633. return;
  634. }
  635. /* try to skip non-SysEx data */
  636. for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
  637. ;
  638. if (pos > 0) {
  639. snd_rawmidi_transmit_ack(substream, pos);
  640. continue;
  641. }
  642. /* look for the start or end marker */
  643. for (end = 1; end < count && tmp[end] < 0xF0; end++)
  644. ;
  645. /* next SysEx started before the end of current one */
  646. if (end < count && tmp[end] == 0xF0) {
  647. /* it's incomplete - drop it */
  648. snd_rawmidi_transmit_ack(substream, end);
  649. continue;
  650. }
  651. /* SysEx complete */
  652. if (end < count && tmp[end] == 0xF7) {
  653. /* queue it, ack it, and get the next one */
  654. count = end + 1;
  655. msg[0] = 0x10 | count;
  656. memcpy(&msg[1], tmp, count);
  657. snd_rawmidi_transmit_ack(substream, count);
  658. urb->transfer_buffer_length += count + 1;
  659. msg += count + 1;
  660. continue;
  661. }
  662. /* less than 9 bytes and no end byte - wait for more */
  663. if (count < MAX_AKAI_SYSEX_LEN) {
  664. ep->ports[0].active = 0;
  665. return;
  666. }
  667. /* 9 bytes and no end marker in sight - malformed, skip it */
  668. snd_rawmidi_transmit_ack(substream, count);
  669. }
  670. }
  671. static struct usb_protocol_ops snd_usbmidi_akai_ops = {
  672. .input = snd_usbmidi_akai_input,
  673. .output = snd_usbmidi_akai_output,
  674. };
  675. /*
  676. * Novation USB MIDI protocol: number of data bytes is in the first byte
  677. * (when receiving) (+1!) or in the second byte (when sending); data begins
  678. * at the third byte.
  679. */
  680. static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
  681. uint8_t* buffer, int buffer_length)
  682. {
  683. if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
  684. return;
  685. snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
  686. }
  687. static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
  688. struct urb *urb)
  689. {
  690. uint8_t* transfer_buffer;
  691. int count;
  692. if (!ep->ports[0].active)
  693. return;
  694. transfer_buffer = urb->transfer_buffer;
  695. count = snd_rawmidi_transmit(ep->ports[0].substream,
  696. &transfer_buffer[2],
  697. ep->max_transfer - 2);
  698. if (count < 1) {
  699. ep->ports[0].active = 0;
  700. return;
  701. }
  702. transfer_buffer[0] = 0;
  703. transfer_buffer[1] = count;
  704. urb->transfer_buffer_length = 2 + count;
  705. }
  706. static struct usb_protocol_ops snd_usbmidi_novation_ops = {
  707. .input = snd_usbmidi_novation_input,
  708. .output = snd_usbmidi_novation_output,
  709. };
  710. /*
  711. * "raw" protocol: just move raw MIDI bytes from/to the endpoint
  712. */
  713. static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
  714. uint8_t* buffer, int buffer_length)
  715. {
  716. snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
  717. }
  718. static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
  719. struct urb *urb)
  720. {
  721. int count;
  722. if (!ep->ports[0].active)
  723. return;
  724. count = snd_rawmidi_transmit(ep->ports[0].substream,
  725. urb->transfer_buffer,
  726. ep->max_transfer);
  727. if (count < 1) {
  728. ep->ports[0].active = 0;
  729. return;
  730. }
  731. urb->transfer_buffer_length = count;
  732. }
  733. static struct usb_protocol_ops snd_usbmidi_raw_ops = {
  734. .input = snd_usbmidi_raw_input,
  735. .output = snd_usbmidi_raw_output,
  736. };
  737. static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
  738. uint8_t *buffer, int buffer_length)
  739. {
  740. if (buffer_length != 9)
  741. return;
  742. buffer_length = 8;
  743. while (buffer_length && buffer[buffer_length - 1] == 0xFD)
  744. buffer_length--;
  745. if (buffer_length)
  746. snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
  747. }
  748. static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
  749. struct urb *urb)
  750. {
  751. int count;
  752. if (!ep->ports[0].active)
  753. return;
  754. switch (snd_usb_get_speed(ep->umidi->dev)) {
  755. case USB_SPEED_HIGH:
  756. case USB_SPEED_SUPER:
  757. count = 1;
  758. break;
  759. default:
  760. count = 2;
  761. }
  762. count = snd_rawmidi_transmit(ep->ports[0].substream,
  763. urb->transfer_buffer,
  764. count);
  765. if (count < 1) {
  766. ep->ports[0].active = 0;
  767. return;
  768. }
  769. memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
  770. urb->transfer_buffer_length = ep->max_transfer;
  771. }
  772. static struct usb_protocol_ops snd_usbmidi_122l_ops = {
  773. .input = snd_usbmidi_us122l_input,
  774. .output = snd_usbmidi_us122l_output,
  775. };
  776. /*
  777. * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
  778. */
  779. static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
  780. {
  781. static const u8 init_data[] = {
  782. /* initialization magic: "get version" */
  783. 0xf0,
  784. 0x00, 0x20, 0x31, /* Emagic */
  785. 0x64, /* Unitor8 */
  786. 0x0b, /* version number request */
  787. 0x00, /* command version */
  788. 0x00, /* EEPROM, box 0 */
  789. 0xf7
  790. };
  791. send_bulk_static_data(ep, init_data, sizeof(init_data));
  792. /* while we're at it, pour on more magic */
  793. send_bulk_static_data(ep, init_data, sizeof(init_data));
  794. }
  795. static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
  796. {
  797. static const u8 finish_data[] = {
  798. /* switch to patch mode with last preset */
  799. 0xf0,
  800. 0x00, 0x20, 0x31, /* Emagic */
  801. 0x64, /* Unitor8 */
  802. 0x10, /* patch switch command */
  803. 0x00, /* command version */
  804. 0x7f, /* to all boxes */
  805. 0x40, /* last preset in EEPROM */
  806. 0xf7
  807. };
  808. send_bulk_static_data(ep, finish_data, sizeof(finish_data));
  809. }
  810. static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
  811. uint8_t* buffer, int buffer_length)
  812. {
  813. int i;
  814. /* FF indicates end of valid data */
  815. for (i = 0; i < buffer_length; ++i)
  816. if (buffer[i] == 0xff) {
  817. buffer_length = i;
  818. break;
  819. }
  820. /* handle F5 at end of last buffer */
  821. if (ep->seen_f5)
  822. goto switch_port;
  823. while (buffer_length > 0) {
  824. /* determine size of data until next F5 */
  825. for (i = 0; i < buffer_length; ++i)
  826. if (buffer[i] == 0xf5)
  827. break;
  828. snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
  829. buffer += i;
  830. buffer_length -= i;
  831. if (buffer_length <= 0)
  832. break;
  833. /* assert(buffer[0] == 0xf5); */
  834. ep->seen_f5 = 1;
  835. ++buffer;
  836. --buffer_length;
  837. switch_port:
  838. if (buffer_length <= 0)
  839. break;
  840. if (buffer[0] < 0x80) {
  841. ep->current_port = (buffer[0] - 1) & 15;
  842. ++buffer;
  843. --buffer_length;
  844. }
  845. ep->seen_f5 = 0;
  846. }
  847. }
  848. static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
  849. struct urb *urb)
  850. {
  851. int port0 = ep->current_port;
  852. uint8_t* buf = urb->transfer_buffer;
  853. int buf_free = ep->max_transfer;
  854. int length, i;
  855. for (i = 0; i < 0x10; ++i) {
  856. /* round-robin, starting at the last current port */
  857. int portnum = (port0 + i) & 15;
  858. struct usbmidi_out_port* port = &ep->ports[portnum];
  859. if (!port->active)
  860. continue;
  861. if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
  862. port->active = 0;
  863. continue;
  864. }
  865. if (portnum != ep->current_port) {
  866. if (buf_free < 2)
  867. break;
  868. ep->current_port = portnum;
  869. buf[0] = 0xf5;
  870. buf[1] = (portnum + 1) & 15;
  871. buf += 2;
  872. buf_free -= 2;
  873. }
  874. if (buf_free < 1)
  875. break;
  876. length = snd_rawmidi_transmit(port->substream, buf, buf_free);
  877. if (length > 0) {
  878. buf += length;
  879. buf_free -= length;
  880. if (buf_free < 1)
  881. break;
  882. }
  883. }
  884. if (buf_free < ep->max_transfer && buf_free > 0) {
  885. *buf = 0xff;
  886. --buf_free;
  887. }
  888. urb->transfer_buffer_length = ep->max_transfer - buf_free;
  889. }
  890. static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
  891. .input = snd_usbmidi_emagic_input,
  892. .output = snd_usbmidi_emagic_output,
  893. .init_out_endpoint = snd_usbmidi_emagic_init_out,
  894. .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
  895. };
  896. static void update_roland_altsetting(struct snd_usb_midi* umidi)
  897. {
  898. struct usb_interface *intf;
  899. struct usb_host_interface *hostif;
  900. struct usb_interface_descriptor *intfd;
  901. int is_light_load;
  902. intf = umidi->iface;
  903. is_light_load = intf->cur_altsetting != intf->altsetting;
  904. if (umidi->roland_load_ctl->private_value == is_light_load)
  905. return;
  906. hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
  907. intfd = get_iface_desc(hostif);
  908. snd_usbmidi_input_stop(&umidi->list);
  909. usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
  910. intfd->bAlternateSetting);
  911. snd_usbmidi_input_start(&umidi->list);
  912. }
  913. static void substream_open(struct snd_rawmidi_substream *substream, int open)
  914. {
  915. struct snd_usb_midi* umidi = substream->rmidi->private_data;
  916. struct snd_kcontrol *ctl;
  917. mutex_lock(&umidi->mutex);
  918. if (open) {
  919. if (umidi->opened++ == 0 && umidi->roland_load_ctl) {
  920. ctl = umidi->roland_load_ctl;
  921. ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  922. snd_ctl_notify(umidi->card,
  923. SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
  924. update_roland_altsetting(umidi);
  925. }
  926. } else {
  927. if (--umidi->opened == 0 && umidi->roland_load_ctl) {
  928. ctl = umidi->roland_load_ctl;
  929. ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  930. snd_ctl_notify(umidi->card,
  931. SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
  932. }
  933. }
  934. mutex_unlock(&umidi->mutex);
  935. }
  936. static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
  937. {
  938. struct snd_usb_midi* umidi = substream->rmidi->private_data;
  939. struct usbmidi_out_port* port = NULL;
  940. int i, j;
  941. int err;
  942. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
  943. if (umidi->endpoints[i].out)
  944. for (j = 0; j < 0x10; ++j)
  945. if (umidi->endpoints[i].out->ports[j].substream == substream) {
  946. port = &umidi->endpoints[i].out->ports[j];
  947. break;
  948. }
  949. if (!port) {
  950. snd_BUG();
  951. return -ENXIO;
  952. }
  953. err = usb_autopm_get_interface(umidi->iface);
  954. if (err < 0)
  955. return -EIO;
  956. substream->runtime->private_data = port;
  957. port->state = STATE_UNKNOWN;
  958. substream_open(substream, 1);
  959. return 0;
  960. }
  961. static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
  962. {
  963. struct snd_usb_midi* umidi = substream->rmidi->private_data;
  964. substream_open(substream, 0);
  965. usb_autopm_put_interface(umidi->iface);
  966. return 0;
  967. }
  968. static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  969. {
  970. struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
  971. port->active = up;
  972. if (up) {
  973. if (port->ep->umidi->disconnected) {
  974. /* gobble up remaining bytes to prevent wait in
  975. * snd_rawmidi_drain_output */
  976. while (!snd_rawmidi_transmit_empty(substream))
  977. snd_rawmidi_transmit_ack(substream, 1);
  978. return;
  979. }
  980. tasklet_schedule(&port->ep->tasklet);
  981. }
  982. }
  983. static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
  984. {
  985. struct usbmidi_out_port* port = substream->runtime->private_data;
  986. struct snd_usb_midi_out_endpoint *ep = port->ep;
  987. unsigned int drain_urbs;
  988. DEFINE_WAIT(wait);
  989. long timeout = msecs_to_jiffies(50);
  990. if (ep->umidi->disconnected)
  991. return;
  992. /*
  993. * The substream buffer is empty, but some data might still be in the
  994. * currently active URBs, so we have to wait for those to complete.
  995. */
  996. spin_lock_irq(&ep->buffer_lock);
  997. drain_urbs = ep->active_urbs;
  998. if (drain_urbs) {
  999. ep->drain_urbs |= drain_urbs;
  1000. do {
  1001. prepare_to_wait(&ep->drain_wait, &wait,
  1002. TASK_UNINTERRUPTIBLE);
  1003. spin_unlock_irq(&ep->buffer_lock);
  1004. timeout = schedule_timeout(timeout);
  1005. spin_lock_irq(&ep->buffer_lock);
  1006. drain_urbs &= ep->drain_urbs;
  1007. } while (drain_urbs && timeout);
  1008. finish_wait(&ep->drain_wait, &wait);
  1009. }
  1010. spin_unlock_irq(&ep->buffer_lock);
  1011. }
  1012. static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
  1013. {
  1014. substream_open(substream, 1);
  1015. return 0;
  1016. }
  1017. static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
  1018. {
  1019. substream_open(substream, 0);
  1020. return 0;
  1021. }
  1022. static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  1023. {
  1024. struct snd_usb_midi* umidi = substream->rmidi->private_data;
  1025. if (up)
  1026. set_bit(substream->number, &umidi->input_triggered);
  1027. else
  1028. clear_bit(substream->number, &umidi->input_triggered);
  1029. }
  1030. static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
  1031. .open = snd_usbmidi_output_open,
  1032. .close = snd_usbmidi_output_close,
  1033. .trigger = snd_usbmidi_output_trigger,
  1034. .drain = snd_usbmidi_output_drain,
  1035. };
  1036. static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
  1037. .open = snd_usbmidi_input_open,
  1038. .close = snd_usbmidi_input_close,
  1039. .trigger = snd_usbmidi_input_trigger
  1040. };
  1041. static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
  1042. unsigned int buffer_length)
  1043. {
  1044. usb_free_coherent(umidi->dev, buffer_length,
  1045. urb->transfer_buffer, urb->transfer_dma);
  1046. usb_free_urb(urb);
  1047. }
  1048. /*
  1049. * Frees an input endpoint.
  1050. * May be called when ep hasn't been initialized completely.
  1051. */
  1052. static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
  1053. {
  1054. unsigned int i;
  1055. for (i = 0; i < INPUT_URBS; ++i)
  1056. if (ep->urbs[i])
  1057. free_urb_and_buffer(ep->umidi, ep->urbs[i],
  1058. ep->urbs[i]->transfer_buffer_length);
  1059. kfree(ep);
  1060. }
  1061. /*
  1062. * Creates an input endpoint.
  1063. */
  1064. static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
  1065. struct snd_usb_midi_endpoint_info* ep_info,
  1066. struct snd_usb_midi_endpoint* rep)
  1067. {
  1068. struct snd_usb_midi_in_endpoint* ep;
  1069. void* buffer;
  1070. unsigned int pipe;
  1071. int length;
  1072. unsigned int i;
  1073. rep->in = NULL;
  1074. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  1075. if (!ep)
  1076. return -ENOMEM;
  1077. ep->umidi = umidi;
  1078. for (i = 0; i < INPUT_URBS; ++i) {
  1079. ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
  1080. if (!ep->urbs[i]) {
  1081. snd_usbmidi_in_endpoint_delete(ep);
  1082. return -ENOMEM;
  1083. }
  1084. }
  1085. if (ep_info->in_interval)
  1086. pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
  1087. else
  1088. pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
  1089. length = usb_maxpacket(umidi->dev, pipe, 0);
  1090. for (i = 0; i < INPUT_URBS; ++i) {
  1091. buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
  1092. &ep->urbs[i]->transfer_dma);
  1093. if (!buffer) {
  1094. snd_usbmidi_in_endpoint_delete(ep);
  1095. return -ENOMEM;
  1096. }
  1097. if (ep_info->in_interval)
  1098. usb_fill_int_urb(ep->urbs[i], umidi->dev,
  1099. pipe, buffer, length,
  1100. snd_usbmidi_in_urb_complete,
  1101. ep, ep_info->in_interval);
  1102. else
  1103. usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
  1104. pipe, buffer, length,
  1105. snd_usbmidi_in_urb_complete, ep);
  1106. ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  1107. }
  1108. rep->in = ep;
  1109. return 0;
  1110. }
  1111. /*
  1112. * Frees an output endpoint.
  1113. * May be called when ep hasn't been initialized completely.
  1114. */
  1115. static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
  1116. {
  1117. unsigned int i;
  1118. for (i = 0; i < OUTPUT_URBS; ++i)
  1119. if (ep->urbs[i].urb) {
  1120. free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
  1121. ep->max_transfer);
  1122. ep->urbs[i].urb = NULL;
  1123. }
  1124. }
  1125. static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
  1126. {
  1127. snd_usbmidi_out_endpoint_clear(ep);
  1128. kfree(ep);
  1129. }
  1130. /*
  1131. * Creates an output endpoint, and initializes output ports.
  1132. */
  1133. static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
  1134. struct snd_usb_midi_endpoint_info* ep_info,
  1135. struct snd_usb_midi_endpoint* rep)
  1136. {
  1137. struct snd_usb_midi_out_endpoint* ep;
  1138. unsigned int i;
  1139. unsigned int pipe;
  1140. void* buffer;
  1141. rep->out = NULL;
  1142. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  1143. if (!ep)
  1144. return -ENOMEM;
  1145. ep->umidi = umidi;
  1146. for (i = 0; i < OUTPUT_URBS; ++i) {
  1147. ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
  1148. if (!ep->urbs[i].urb) {
  1149. snd_usbmidi_out_endpoint_delete(ep);
  1150. return -ENOMEM;
  1151. }
  1152. ep->urbs[i].ep = ep;
  1153. }
  1154. if (ep_info->out_interval)
  1155. pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
  1156. else
  1157. pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
  1158. switch (umidi->usb_id) {
  1159. default:
  1160. ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
  1161. break;
  1162. /*
  1163. * Various chips declare a packet size larger than 4 bytes, but
  1164. * do not actually work with larger packets:
  1165. */
  1166. case USB_ID(0x0a92, 0x1020): /* ESI M4U */
  1167. case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
  1168. case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
  1169. case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
  1170. case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
  1171. case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */
  1172. ep->max_transfer = 4;
  1173. break;
  1174. /*
  1175. * Some devices only work with 9 bytes packet size:
  1176. */
  1177. case USB_ID(0x0644, 0x800E): /* Tascam US-122L */
  1178. case USB_ID(0x0644, 0x800F): /* Tascam US-144 */
  1179. ep->max_transfer = 9;
  1180. break;
  1181. }
  1182. for (i = 0; i < OUTPUT_URBS; ++i) {
  1183. buffer = usb_alloc_coherent(umidi->dev,
  1184. ep->max_transfer, GFP_KERNEL,
  1185. &ep->urbs[i].urb->transfer_dma);
  1186. if (!buffer) {
  1187. snd_usbmidi_out_endpoint_delete(ep);
  1188. return -ENOMEM;
  1189. }
  1190. if (ep_info->out_interval)
  1191. usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
  1192. pipe, buffer, ep->max_transfer,
  1193. snd_usbmidi_out_urb_complete,
  1194. &ep->urbs[i], ep_info->out_interval);
  1195. else
  1196. usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
  1197. pipe, buffer, ep->max_transfer,
  1198. snd_usbmidi_out_urb_complete,
  1199. &ep->urbs[i]);
  1200. ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  1201. }
  1202. spin_lock_init(&ep->buffer_lock);
  1203. tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
  1204. init_waitqueue_head(&ep->drain_wait);
  1205. for (i = 0; i < 0x10; ++i)
  1206. if (ep_info->out_cables & (1 << i)) {
  1207. ep->ports[i].ep = ep;
  1208. ep->ports[i].cable = i << 4;
  1209. }
  1210. if (umidi->usb_protocol_ops->init_out_endpoint)
  1211. umidi->usb_protocol_ops->init_out_endpoint(ep);
  1212. rep->out = ep;
  1213. return 0;
  1214. }
  1215. /*
  1216. * Frees everything.
  1217. */
  1218. static void snd_usbmidi_free(struct snd_usb_midi* umidi)
  1219. {
  1220. int i;
  1221. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1222. struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
  1223. if (ep->out)
  1224. snd_usbmidi_out_endpoint_delete(ep->out);
  1225. if (ep->in)
  1226. snd_usbmidi_in_endpoint_delete(ep->in);
  1227. }
  1228. mutex_destroy(&umidi->mutex);
  1229. kfree(umidi);
  1230. }
  1231. /*
  1232. * Unlinks all URBs (must be done before the usb_device is deleted).
  1233. */
  1234. void snd_usbmidi_disconnect(struct list_head* p)
  1235. {
  1236. struct snd_usb_midi* umidi;
  1237. unsigned int i, j;
  1238. umidi = list_entry(p, struct snd_usb_midi, list);
  1239. /*
  1240. * an URB's completion handler may start the timer and
  1241. * a timer may submit an URB. To reliably break the cycle
  1242. * a flag under lock must be used
  1243. */
  1244. spin_lock_irq(&umidi->disc_lock);
  1245. umidi->disconnected = 1;
  1246. spin_unlock_irq(&umidi->disc_lock);
  1247. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1248. struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
  1249. if (ep->out)
  1250. tasklet_kill(&ep->out->tasklet);
  1251. if (ep->out) {
  1252. for (j = 0; j < OUTPUT_URBS; ++j)
  1253. usb_kill_urb(ep->out->urbs[j].urb);
  1254. if (umidi->usb_protocol_ops->finish_out_endpoint)
  1255. umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
  1256. ep->out->active_urbs = 0;
  1257. if (ep->out->drain_urbs) {
  1258. ep->out->drain_urbs = 0;
  1259. wake_up(&ep->out->drain_wait);
  1260. }
  1261. }
  1262. if (ep->in)
  1263. for (j = 0; j < INPUT_URBS; ++j)
  1264. usb_kill_urb(ep->in->urbs[j]);
  1265. /* free endpoints here; later call can result in Oops */
  1266. if (ep->out)
  1267. snd_usbmidi_out_endpoint_clear(ep->out);
  1268. if (ep->in) {
  1269. snd_usbmidi_in_endpoint_delete(ep->in);
  1270. ep->in = NULL;
  1271. }
  1272. }
  1273. del_timer_sync(&umidi->error_timer);
  1274. }
  1275. static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
  1276. {
  1277. struct snd_usb_midi* umidi = rmidi->private_data;
  1278. snd_usbmidi_free(umidi);
  1279. }
  1280. static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
  1281. int stream, int number)
  1282. {
  1283. struct list_head* list;
  1284. list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
  1285. struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
  1286. if (substream->number == number)
  1287. return substream;
  1288. }
  1289. return NULL;
  1290. }
  1291. /*
  1292. * This list specifies names for ports that do not fit into the standard
  1293. * "(product) MIDI (n)" schema because they aren't external MIDI ports,
  1294. * such as internal control or synthesizer ports.
  1295. */
  1296. static struct port_info {
  1297. u32 id;
  1298. short int port;
  1299. short int voices;
  1300. const char *name;
  1301. unsigned int seq_flags;
  1302. } snd_usbmidi_port_info[] = {
  1303. #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
  1304. { .id = USB_ID(vendor, product), \
  1305. .port = num, .voices = voices_, \
  1306. .name = name_, .seq_flags = flags }
  1307. #define EXTERNAL_PORT(vendor, product, num, name) \
  1308. PORT_INFO(vendor, product, num, name, 0, \
  1309. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1310. SNDRV_SEQ_PORT_TYPE_HARDWARE | \
  1311. SNDRV_SEQ_PORT_TYPE_PORT)
  1312. #define CONTROL_PORT(vendor, product, num, name) \
  1313. PORT_INFO(vendor, product, num, name, 0, \
  1314. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1315. SNDRV_SEQ_PORT_TYPE_HARDWARE)
  1316. #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
  1317. PORT_INFO(vendor, product, num, name, voices, \
  1318. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1319. SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
  1320. SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
  1321. SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
  1322. SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
  1323. SNDRV_SEQ_PORT_TYPE_HARDWARE | \
  1324. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
  1325. #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
  1326. PORT_INFO(vendor, product, num, name, voices, \
  1327. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1328. SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
  1329. SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
  1330. SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
  1331. SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
  1332. SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
  1333. SNDRV_SEQ_PORT_TYPE_HARDWARE | \
  1334. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
  1335. /* Roland UA-100 */
  1336. CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
  1337. /* Roland SC-8850 */
  1338. SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
  1339. SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
  1340. SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
  1341. SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
  1342. EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
  1343. EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
  1344. /* Roland U-8 */
  1345. EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
  1346. CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
  1347. /* Roland SC-8820 */
  1348. SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
  1349. SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
  1350. EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
  1351. /* Roland SK-500 */
  1352. SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
  1353. SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
  1354. EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
  1355. /* Roland SC-D70 */
  1356. SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
  1357. SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
  1358. EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
  1359. /* Edirol UM-880 */
  1360. CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
  1361. /* Edirol SD-90 */
  1362. ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
  1363. ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
  1364. EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
  1365. EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
  1366. /* Edirol UM-550 */
  1367. CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
  1368. /* Edirol SD-20 */
  1369. ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
  1370. ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
  1371. EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
  1372. /* Edirol SD-80 */
  1373. ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
  1374. ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
  1375. EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
  1376. EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
  1377. /* Edirol UA-700 */
  1378. EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
  1379. CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
  1380. /* Roland VariOS */
  1381. EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
  1382. EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
  1383. EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
  1384. /* Edirol PCR */
  1385. EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
  1386. EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
  1387. EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
  1388. /* BOSS GS-10 */
  1389. EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
  1390. CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
  1391. /* Edirol UA-1000 */
  1392. EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
  1393. CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
  1394. /* Edirol UR-80 */
  1395. EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
  1396. EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
  1397. EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
  1398. /* Edirol PCR-A */
  1399. EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
  1400. EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
  1401. EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
  1402. /* Edirol UM-3EX */
  1403. CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
  1404. /* M-Audio MidiSport 8x8 */
  1405. CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
  1406. CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
  1407. /* MOTU Fastlane */
  1408. EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
  1409. EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
  1410. /* Emagic Unitor8/AMT8/MT4 */
  1411. EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
  1412. EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
  1413. EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
  1414. /* Akai MPD16 */
  1415. CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
  1416. PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
  1417. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
  1418. SNDRV_SEQ_PORT_TYPE_HARDWARE),
  1419. /* Access Music Virus TI */
  1420. EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
  1421. PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
  1422. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
  1423. SNDRV_SEQ_PORT_TYPE_HARDWARE |
  1424. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
  1425. };
  1426. static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
  1427. {
  1428. int i;
  1429. for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
  1430. if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
  1431. snd_usbmidi_port_info[i].port == number)
  1432. return &snd_usbmidi_port_info[i];
  1433. }
  1434. return NULL;
  1435. }
  1436. static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
  1437. struct snd_seq_port_info *seq_port_info)
  1438. {
  1439. struct snd_usb_midi *umidi = rmidi->private_data;
  1440. struct port_info *port_info;
  1441. /* TODO: read port flags from descriptors */
  1442. port_info = find_port_info(umidi, number);
  1443. if (port_info) {
  1444. seq_port_info->type = port_info->seq_flags;
  1445. seq_port_info->midi_voices = port_info->voices;
  1446. }
  1447. }
  1448. static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
  1449. int stream, int number,
  1450. struct snd_rawmidi_substream ** rsubstream)
  1451. {
  1452. struct port_info *port_info;
  1453. const char *name_format;
  1454. struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
  1455. if (!substream) {
  1456. snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
  1457. return;
  1458. }
  1459. /* TODO: read port name from jack descriptor */
  1460. port_info = find_port_info(umidi, number);
  1461. name_format = port_info ? port_info->name : "%s MIDI %d";
  1462. snprintf(substream->name, sizeof(substream->name),
  1463. name_format, umidi->card->shortname, number + 1);
  1464. *rsubstream = substream;
  1465. }
  1466. /*
  1467. * Creates the endpoints and their ports.
  1468. */
  1469. static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
  1470. struct snd_usb_midi_endpoint_info* endpoints)
  1471. {
  1472. int i, j, err;
  1473. int out_ports = 0, in_ports = 0;
  1474. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1475. if (endpoints[i].out_cables) {
  1476. err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
  1477. &umidi->endpoints[i]);
  1478. if (err < 0)
  1479. return err;
  1480. }
  1481. if (endpoints[i].in_cables) {
  1482. err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
  1483. &umidi->endpoints[i]);
  1484. if (err < 0)
  1485. return err;
  1486. }
  1487. for (j = 0; j < 0x10; ++j) {
  1488. if (endpoints[i].out_cables & (1 << j)) {
  1489. snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
  1490. &umidi->endpoints[i].out->ports[j].substream);
  1491. ++out_ports;
  1492. }
  1493. if (endpoints[i].in_cables & (1 << j)) {
  1494. snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
  1495. &umidi->endpoints[i].in->ports[j].substream);
  1496. ++in_ports;
  1497. }
  1498. }
  1499. }
  1500. snd_printdd(KERN_INFO "created %d output and %d input ports\n",
  1501. out_ports, in_ports);
  1502. return 0;
  1503. }
  1504. /*
  1505. * Returns MIDIStreaming device capabilities.
  1506. */
  1507. static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
  1508. struct snd_usb_midi_endpoint_info* endpoints)
  1509. {
  1510. struct usb_interface* intf;
  1511. struct usb_host_interface *hostif;
  1512. struct usb_interface_descriptor* intfd;
  1513. struct usb_ms_header_descriptor* ms_header;
  1514. struct usb_host_endpoint *hostep;
  1515. struct usb_endpoint_descriptor* ep;
  1516. struct usb_ms_endpoint_descriptor* ms_ep;
  1517. int i, epidx;
  1518. intf = umidi->iface;
  1519. if (!intf)
  1520. return -ENXIO;
  1521. hostif = &intf->altsetting[0];
  1522. intfd = get_iface_desc(hostif);
  1523. ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
  1524. if (hostif->extralen >= 7 &&
  1525. ms_header->bLength >= 7 &&
  1526. ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
  1527. ms_header->bDescriptorSubtype == UAC_HEADER)
  1528. snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
  1529. ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
  1530. else
  1531. snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
  1532. epidx = 0;
  1533. for (i = 0; i < intfd->bNumEndpoints; ++i) {
  1534. hostep = &hostif->endpoint[i];
  1535. ep = get_ep_desc(hostep);
  1536. if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
  1537. continue;
  1538. ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
  1539. if (hostep->extralen < 4 ||
  1540. ms_ep->bLength < 4 ||
  1541. ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
  1542. ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
  1543. continue;
  1544. if (usb_endpoint_dir_out(ep)) {
  1545. if (endpoints[epidx].out_ep) {
  1546. if (++epidx >= MIDI_MAX_ENDPOINTS) {
  1547. snd_printk(KERN_WARNING "too many endpoints\n");
  1548. break;
  1549. }
  1550. }
  1551. endpoints[epidx].out_ep = usb_endpoint_num(ep);
  1552. if (usb_endpoint_xfer_int(ep))
  1553. endpoints[epidx].out_interval = ep->bInterval;
  1554. else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
  1555. /*
  1556. * Low speed bulk transfers don't exist, so
  1557. * force interrupt transfers for devices like
  1558. * ESI MIDI Mate that try to use them anyway.
  1559. */
  1560. endpoints[epidx].out_interval = 1;
  1561. endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
  1562. snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
  1563. ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
  1564. } else {
  1565. if (endpoints[epidx].in_ep) {
  1566. if (++epidx >= MIDI_MAX_ENDPOINTS) {
  1567. snd_printk(KERN_WARNING "too many endpoints\n");
  1568. break;
  1569. }
  1570. }
  1571. endpoints[epidx].in_ep = usb_endpoint_num(ep);
  1572. if (usb_endpoint_xfer_int(ep))
  1573. endpoints[epidx].in_interval = ep->bInterval;
  1574. else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
  1575. endpoints[epidx].in_interval = 1;
  1576. endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
  1577. snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
  1578. ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
  1579. }
  1580. }
  1581. return 0;
  1582. }
  1583. static int roland_load_info(struct snd_kcontrol *kcontrol,
  1584. struct snd_ctl_elem_info *info)
  1585. {
  1586. static const char *const names[] = { "High Load", "Light Load" };
  1587. return snd_ctl_enum_info(info, 1, 2, names);
  1588. }
  1589. static int roland_load_get(struct snd_kcontrol *kcontrol,
  1590. struct snd_ctl_elem_value *value)
  1591. {
  1592. value->value.enumerated.item[0] = kcontrol->private_value;
  1593. return 0;
  1594. }
  1595. static int roland_load_put(struct snd_kcontrol *kcontrol,
  1596. struct snd_ctl_elem_value *value)
  1597. {
  1598. struct snd_usb_midi* umidi = kcontrol->private_data;
  1599. int changed;
  1600. if (value->value.enumerated.item[0] > 1)
  1601. return -EINVAL;
  1602. mutex_lock(&umidi->mutex);
  1603. changed = value->value.enumerated.item[0] != kcontrol->private_value;
  1604. if (changed)
  1605. kcontrol->private_value = value->value.enumerated.item[0];
  1606. mutex_unlock(&umidi->mutex);
  1607. return changed;
  1608. }
  1609. static struct snd_kcontrol_new roland_load_ctl = {
  1610. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1611. .name = "MIDI Input Mode",
  1612. .info = roland_load_info,
  1613. .get = roland_load_get,
  1614. .put = roland_load_put,
  1615. .private_value = 1,
  1616. };
  1617. /