OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [usb/] [usb-midi.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
  usb-midi.c  --  USB-MIDI driver
3
 
4
  Copyright (C) 2001
5
      NAGANO Daisuke <breeze.nagano@nifty.ne.jp>
6
 
7
  This program is free software; you can redistribute it and/or modify
8
  it under the terms of the GNU General Public License as published by
9
  the Free Software Foundation; either version 2, or (at your option)
10
  any later version.
11
 
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU General Public License for more details.
16
 
17
  You should have received a copy of the GNU General Public License
18
  along with this program; if not, write to the Free Software
19
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
 
21
  This driver is based on:
22
    - 'Universal Serial Bus Device Class Definition for MIDI Device'
23
    - linux/drivers/sound/es1371.c, linux/drivers/usb/audio.c
24
    - alsa/lowlevel/pci/cs64xx.c
25
    - umidi.c for NetBSD
26
 */
27
 
28
/* ------------------------------------------------------------------------- */
29
 
30
 
31
#include <linux/module.h>
32
#include <linux/kernel.h>
33
#include <linux/sched.h>
34
#include <linux/list.h>
35
#include <linux/slab.h>
36
#include <linux/wrapper.h>
37
#include <linux/usb.h>
38
#include <linux/poll.h>
39
#include <linux/sound.h>
40
#include <linux/init.h>
41
#include <asm/semaphore.h>
42
 
43
/** This declaration is missing from linux/usb.h **/
44
extern int usb_get_string(struct usb_device *dev, unsigned short langid, unsigned char index, void *buf, int size);
45
 
46
#include "usb-midi.h"
47
 
48
/* ------------------------------------------------------------------------- */
49
 
50
/* More verbose on syslog */
51
#undef MIDI_DEBUG
52
 
53
#define MIDI_IN_BUFSIZ 1024
54
 
55
#define HAVE_SUPPORT_USB_MIDI_CLASS
56
 
57
#undef HAVE_SUPPORT_ALSA
58
 
59
#undef MOD_INC_EACH_PROBE
60
 
61
/* ------------------------------------------------------------------------- */
62
 
63
static int singlebyte = 0;
64
MODULE_PARM(singlebyte,"i");
65
MODULE_PARM_DESC(singlebyte,"Enable sending MIDI messages with single message packet");
66
 
67
static int maxdevices = 4;
68
MODULE_PARM(maxdevices,"i");
69
MODULE_PARM_DESC(maxdevices,"Max number of allocatable MIDI device");
70
 
71
static int uvendor     = -1;
72
MODULE_PARM(uvendor,"i");
73
MODULE_PARM_DESC(uvendor, "The USB Vendor ID of a semi-compliant interface");
74
 
75
static int uproduct    = -1;
76
MODULE_PARM(uproduct,"i");
77
MODULE_PARM_DESC(uproduct, "The USB Product ID of a semi-compliant interface");
78
 
79
static int uinterface  = -1;
80
MODULE_PARM(uinterface,"i");
81
MODULE_PARM_DESC(uinterface, "The Interface number of a semi-compliant interface");
82
 
83
static int ualt        = -1;
84
MODULE_PARM(ualt,"i");
85
MODULE_PARM_DESC(ualt, "The optional alternative setting of a semi-compliant interface");
86
 
87
static int umin        = -1;
88
MODULE_PARM(umin,"i");
89
MODULE_PARM_DESC(umin, "The input endpoint of a semi-compliant interface");
90
 
91
static int umout       = -1;
92
MODULE_PARM(umout,"i");
93
MODULE_PARM_DESC(umout, "The output endpoint of a semi-compliant interface");
94
 
95
static int ucable      = -1;
96
MODULE_PARM(ucable,"i");
97
MODULE_PARM_DESC(ucable, "The cable number used for a semi-compliant interface");
98
 
99
/** Note -- the usb_string() returns only Latin-1 characters.
100
 * (unicode chars <= 255). To support Japanese, a unicode16LE-to-EUC or
101
 * unicode16LE-to-JIS routine is needed to wrap around usb_get_string().
102
 **/
103
static unsigned short ulangid      = 0x0409; /** 0x0411 for Japanese **/
104
MODULE_PARM(ulangid,"h");
105
MODULE_PARM_DESC(ulangid, "The optional preferred USB Language ID for all devices");
106
 
107
MODULE_AUTHOR("NAGANO Daisuke <breeze.nagano@nifty.ne.jp>");
108
MODULE_DESCRIPTION("USB-MIDI driver");
109
#if LINUX_VERSION_CODE  >= KERNEL_VERSION(2,4,14)
110
MODULE_LICENSE("GPL");
111
#endif
112
 
113
/* ------------------------------------------------------------------------- */
114
 
115
/** MIDIStreaming Class-Specific Interface Descriptor Subtypes **/
116
 
117
#define MS_DESCRIPTOR_UNDEFINED 0
118
#define MS_HEADER               1
119
#define MIDI_IN_JACK            2
120
#define MIDI_OUT_JACK           3
121
/* Spec reads: ELEMENT */
122
#define ELEMENT_DESCRIPTOR      4
123
 
124
#define MS_HEADER_LENGTH        7
125
 
126
/** MIDIStreaming Class-Specific Endpoint Descriptor Subtypes **/
127
 
128
#define DESCRIPTOR_UNDEFINED    0
129
/* Spec reads: MS_GENERAL */
130
#define MS_GENERAL_ENDPOINT     1
131
 
132
/** MIDIStreaming MIDI IN and OUT Jack Types **/
133
 
134
#define JACK_TYPE_UNDEFINED     0
135
/* Spec reads: EMBEDDED */
136
#define EMBEDDED_JACK           1
137
/* Spec reads: EXTERNAL */
138
#define EXTERNAL_JACK           2
139
 
140
 
141
/* structure summary
142
 
143
      usb_midi_state     usb_device
144
       |         |
145
      *|        *|       per ep
146
     in_ep     out_ep
147
       |         |
148
      *|        *|       per cable
149
      min       mout
150
       |         |       (cable to device pairing magic)
151
       |         |
152
       usb_midi_dev      dev_id (major,minor) == file->private_data
153
 
154
*/
155
 
156
/* usb_midi_state: corresponds to a USB-MIDI module */
157
struct usb_midi_state {
158
        struct list_head   mididev;
159
 
160
        struct usb_device *usbdev;
161
 
162
        struct list_head   midiDevList;
163
        struct list_head   inEndpointList;
164
        struct list_head   outEndpointList;
165
 
166
        spinlock_t         lock;
167
 
168
        unsigned int       count; /* usage counter */
169
};
170
 
171
/* midi_out_endpoint: corresponds to an output endpoint */
172
struct midi_out_endpoint {
173
        struct list_head  list;
174
 
175
        struct usb_device *usbdev;
176
        int                endpoint;
177
        spinlock_t         lock;
178
        wait_queue_head_t  wait;
179
 
180
        unsigned char     *buf;
181
        int                bufWrPtr;
182
        int                bufSize;
183
 
184
        struct urb       *urb;
185
};
186
 
187
/* midi_in_endpoint: corresponds to an input endpoint */
188
struct midi_in_endpoint {
189
        struct list_head   list;
190
 
191
        struct usb_device *usbdev;
192
        int                endpoint;
193
        spinlock_t         lock;
194
        wait_queue_head_t  wait;
195
 
196
        struct usb_mididev *cables[16]; // cables open for read
197
        int                 readers;    // number of cables open for read
198
 
199
        struct urb        *urb;
200
        unsigned char     *recvBuf;
201
        int                recvBufSize;
202
        int                urbSubmitted;        //FIXME: == readers > 0
203
};
204
 
205
/* usb_mididev: corresponds to a logical device */
206
struct usb_mididev {
207
        struct list_head       list;
208
 
209
        struct usb_midi_state *midi;
210
        int                    dev_midi;
211
        mode_t                 open_mode;
212
 
213
        struct {
214
                struct midi_in_endpoint *ep;
215
                int              cableId;
216
 
217
// as we are pushing data from usb_bulk_read to usb_midi_read,
218
// we need a larger, cyclic buffer here.
219
                unsigned char    buf[MIDI_IN_BUFSIZ];
220
                int              bufRdPtr;
221
                int              bufWrPtr;
222
                int              bufRemains;
223
        } min;
224
 
225
        struct {
226
                struct midi_out_endpoint *ep;
227
                int              cableId;
228
 
229
                unsigned char    buf[3];
230
                int              bufPtr;
231
                int              bufRemains;
232
 
233
                int              isInExclusive;
234
                unsigned char    lastEvent;
235
        } mout;
236
 
237
        int singlebyte;
238
};
239
 
240
/** Map the high nybble of MIDI voice messages to number of Message bytes.
241
 * High nyble ranges from 0x8 to 0xe
242
 */
243
 
244
static int remains_80e0[] = {
245
        3,      /** 0x8X Note Off **/
246
        3,      /** 0x9X Note On **/
247
        3,      /** 0xAX Poly-key pressure **/
248
        3,      /** 0xBX Control Change **/
249
        2,      /** 0xCX Program Change **/
250
        2,      /** 0xDX Channel pressure **/
251
        3       /** 0xEX PitchBend Change **/
252
};
253
 
254
/** Map the messages to a number of Message bytes.
255
 *
256
 **/
257
static int remains_f0f6[] = {
258
        0,       /** 0xF0 **/
259
        2,      /** 0XF1 **/
260
        3,      /** 0XF2 **/
261
        2,      /** 0XF3 **/
262
        2,      /** 0XF4 (Undefined by MIDI Spec, and subject to change) **/
263
        2,      /** 0XF5 (Undefined by MIDI Spec, and subject to change) **/
264
        1       /** 0XF6 **/
265
};
266
 
267
/** Map the messages to a CIN (Code Index Number).
268
 *
269
 **/
270
static int cin_f0ff[] = {
271
        4,      /** 0xF0 System Exclusive Message Start (special cases may be 6 or 7) */
272
        2,      /** 0xF1 **/
273
        3,      /** 0xF2 **/
274
        2,      /** 0xF3 **/
275
        2,      /** 0xF4 **/
276
        2,      /** 0xF5 **/
277
        5,      /** 0xF6 **/
278
        5,      /** 0xF7 End of System Exclusive Message (May be 6 or 7) **/
279
        5,      /** 0xF8 **/
280
        5,      /** 0xF9 **/
281
        5,      /** 0xFA **/
282
        5,      /** 0xFB **/
283
        5,      /** 0xFC **/
284
        5,      /** 0xFD **/
285
        5,      /** 0xFE **/
286
        5       /** 0xFF **/
287
};
288
 
289
/** Map MIDIStreaming Event packet Code Index Number (low nybble of byte 0)
290
 * to the number of bytes of valid MIDI data.
291
 *
292
 * CIN of 0 and 1 are NOT USED in MIDIStreaming 1.0.
293
 *
294
 **/
295
static int cin_to_len[] = {
296
        0, 0, 2, 3,
297
        3, 1, 2, 3,
298
        3, 3, 3, 3,
299
        2, 2, 3, 1
300
};
301
 
302
 
303
/* ------------------------------------------------------------------------- */
304
 
305
static struct list_head mididevs = LIST_HEAD_INIT(mididevs);
306
 
307
static DECLARE_MUTEX(open_sem);
308
static DECLARE_WAIT_QUEUE_HEAD(open_wait);
309
 
310
 
311
/* ------------------------------------------------------------------------- */
312
 
313
static void usb_write_callback(struct urb *urb)
314
{
315
        struct midi_out_endpoint *ep = (struct midi_out_endpoint *)urb->context;
316
 
317
        if ( waitqueue_active( &ep->wait ) )
318
                wake_up_interruptible( &ep->wait );
319
}
320
 
321
 
322
static int usb_write( struct midi_out_endpoint *ep, unsigned char *buf, int len )
323
{
324
        struct usb_device *d;
325
        int pipe;
326
        int ret = 0;
327
        int status;
328
        int maxretry = 50;
329
 
330
        DECLARE_WAITQUEUE(wait,current);
331
        init_waitqueue_head(&ep->wait);
332
 
333
        d = ep->usbdev;
334
        pipe = usb_sndbulkpipe(d, ep->endpoint);
335
        FILL_BULK_URB( ep->urb, d, pipe, (unsigned char*)buf, len,
336
                       (usb_complete_t)usb_write_callback, ep );
337
 
338
        status = usb_submit_urb(ep->urb);
339
 
340
        if (status) {
341
                printk(KERN_ERR "usbmidi: Cannot submit urb (%d)\n",status);
342
                ret = -EFAULT;
343
        }
344
 
345
        add_wait_queue( &ep->wait, &wait );
346
        set_current_state( TASK_INTERRUPTIBLE );
347
 
348
        while( ep->urb->status == -EINPROGRESS ) {
349
                if ( maxretry-- < 0 ) {
350
                        printk(KERN_ERR "usbmidi: usb_bulk_msg timed out\n");
351
                        ret = -ETIME;
352
                        break;
353
                }
354
                interruptible_sleep_on_timeout( &ep->wait, 10 );
355
        }
356
        set_current_state( TASK_RUNNING );
357
        remove_wait_queue( &ep->wait, &wait );
358
 
359
        return ret;
360
}
361
 
362
 
363
/** Copy data from URB to In endpoint buf.
364
 * Discard if CIN == 0 or CIN = 1.
365
 *
366
 *
367
 **/
368
 
369
static void usb_bulk_read(struct urb *urb)
370
{
371
        struct midi_in_endpoint *ep = (struct midi_in_endpoint *)(urb->context);
372
        unsigned char *data = urb->transfer_buffer;
373
        int i, j, wake;
374
        unsigned long int flags;
375
 
376
        if ( !ep->urbSubmitted ) {
377
                return;
378
        }
379
 
380
        if ( (urb->status == 0) && (urb->actual_length > 0) ) {
381
                wake = 0;
382
                spin_lock_irqsave( &ep->lock, flags );
383
 
384
                for(j = 0; j < urb->actual_length; j += 4) {
385
                        int cin = (data[j]>>0)&0xf;
386
                        int cab = (data[j]>>4)&0xf;
387
                        struct usb_mididev *cable = ep->cables[cab];
388
                        if ( cable ) {
389
                                int len = cin_to_len[cin]; /** length of MIDI data **/
390
                                for (i = 0; i < len; i++) {
391
                                        cable->min.buf[cable->min.bufWrPtr] = data[1+i+j];
392
                                        cable->min.bufWrPtr = (cable->min.bufWrPtr+1)%MIDI_IN_BUFSIZ;
393
                                        if (cable->min.bufRemains < MIDI_IN_BUFSIZ)
394
                                                cable->min.bufRemains += 1;
395
                                        else /** need to drop data **/
396
                                                cable->min.bufRdPtr += (cable->min.bufRdPtr+1)%MIDI_IN_BUFSIZ;
397
                                        wake = 1;
398
                                }
399
                        }
400
                }
401
 
402
                spin_unlock_irqrestore( &ep->lock, flags );
403
                if ( wake ) {
404
                        wake_up( &ep->wait );
405
                }
406
        }
407
 
408
        /* urb->dev must be reinitialized on 2.4.x kernels */
409
        urb->dev = ep->usbdev;
410
 
411
        urb->actual_length = 0;
412
        usb_submit_urb(urb);
413
}
414
 
415
 
416
 
417
/* ------------------------------------------------------------------------- */
418
 
419
/* This routine must be called with spin_lock */
420
 
421
/** Wrapper around usb_write().
422
 *  This routine must be called with spin_lock held on ep.
423
 *  Called by midiWrite(), putOneMidiEvent(), and  usb_midi_write();
424
 **/
425
static int flush_midi_buffer( struct midi_out_endpoint *ep )
426
{
427
        int ret=0;
428
 
429
        if ( ep->bufWrPtr > 0 ) {
430
                ret = usb_write( ep, ep->buf, ep->bufWrPtr );
431
                ep->bufWrPtr = 0;
432
        }
433
 
434
        return ret;
435
}
436
 
437
 
438
/* ------------------------------------------------------------------------- */
439
 
440
 
441
/** Given a MIDI Event, determine size of data to be attached to
442
 * USB-MIDI packet.
443
 * Returns 1, 2 or 3.
444
 * Called by midiWrite();
445
 * Uses remains_80e0 and remains_f0f6;
446
 **/
447
static int get_remains(int event)
448
{
449
        int ret;
450
 
451
        if ( event  < 0x80 ) {
452
                ret = 1;
453
        } else if ( event < 0xf0 ) {
454
                ret = remains_80e0[((event-0x80)>>4)&0x0f];
455
        } else if ( event < 0xf7 ) {
456
                ret = remains_f0f6[event-0xf0];
457
        } else {
458
                ret = 1;
459
        }
460
 
461
        return ret;
462
}
463
 
464
/** Given the output MIDI data in the output buffer, computes a reasonable
465
 * CIN.
466
 * Called by putOneMidiEvent().
467
 **/
468
static int get_CIN( struct usb_mididev *m )
469
{
470
        int cin;
471
 
472
        if ( m->mout.buf[0] == 0xf7 ) {
473
                cin = 5;
474
        }
475
        else if ( m->mout.buf[1] == 0xf7 ) {
476
                cin = 6;
477
        }
478
        else if ( m->mout.buf[2] == 0xf7 ) {
479
                cin = 7;
480
        }
481
        else {
482
                if ( m->mout.isInExclusive == 1 ) {
483
                        cin = 4;
484
                } else if ( m->mout.buf[0] < 0x80 ) {
485
                        /** One byte that we know nothing about. **/
486
                        cin = 0xF;
487
                } else if ( m->mout.buf[0] < 0xf0 ) {
488
                        /** MIDI Voice messages 0x8X to 0xEX map to cin 0x8 to 0xE. **/
489
                        cin = (m->mout.buf[0]>>4)&0x0f;
490
                }
491
                else {
492
                        /** Special lookup table exists for real-time events. **/
493
                        cin = cin_f0ff[m->mout.buf[0]-0xf0];
494
                }
495
        }
496
 
497
        return cin;
498
}
499
 
500
 
501
/* ------------------------------------------------------------------------- */
502
 
503
 
504
 
505
/** Move data to USB endpoint buffer.
506
 *
507
 **/
508
static int put_one_midi_event(struct usb_mididev *m)
509
{
510
        int cin;
511
        unsigned long flags;
512
        struct midi_out_endpoint *ep = m->mout.ep;
513
        int ret=0;
514
 
515
        cin = get_CIN( m );
516
        if ( cin > 0x0f || cin < 0 ) {
517
                return -EINVAL;
518
        }
519
 
520
        spin_lock_irqsave( &ep->lock, flags );
521
        ep->buf[ep->bufWrPtr++] = (m->mout.cableId<<4) | cin;
522
        ep->buf[ep->bufWrPtr++] = m->mout.buf[0];
523
        ep->buf[ep->bufWrPtr++] = m->mout.buf[1];
524
        ep->buf[ep->bufWrPtr++] = m->mout.buf[2];
525
        if ( ep->bufWrPtr >= ep->bufSize ) {
526
                ret = flush_midi_buffer( ep );
527
        }
528
        spin_unlock_irqrestore( &ep->lock, flags);
529
 
530
        m->mout.buf[0] = m->mout.buf[1] = m->mout.buf[2] = 0;
531
        m->mout.bufPtr = 0;
532
 
533
        return ret;
534
}
535
 
536
/** Write the MIDI message v on the midi device.
537
 *  Called by usb_midi_write();
538
 *  Responsible for packaging a MIDI data stream into USB-MIDI packets.
539
 **/
540
 
541
static int midi_write( struct usb_mididev *m, int v )
542
{
543
        unsigned long flags;
544
        struct midi_out_endpoint *ep = m->mout.ep;
545
        int ret=0;
546
        unsigned char c = (unsigned char)v;
547
        unsigned char sysrt_buf[4];
548
 
549
        if ( m->singlebyte != 0 ) {
550
                /** Simple code to handle the single-byte USB-MIDI protocol. */
551
                spin_lock_irqsave( &ep->lock, flags );
552
                if ( ep->bufWrPtr+4 > ep->bufSize ) {
553
                        ret = flush_midi_buffer( ep );
554
                        if ( !ret ) {
555
                                spin_unlock_irqrestore( &ep->lock, flags );
556
                                return ret;
557
                        }
558
                }
559
                ep->buf[ep->bufWrPtr++] = (m->mout.cableId<<4) |  0x0f; /* single byte */
560
                ep->buf[ep->bufWrPtr++] = c;
561
                ep->buf[ep->bufWrPtr++] = 0;
562
                ep->buf[ep->bufWrPtr++] = 0;
563
                if ( ep->bufWrPtr >= ep->bufSize ) {
564
                        ret = flush_midi_buffer( ep );
565
                }
566
                spin_unlock_irqrestore( &ep->lock, flags );
567
 
568
                return ret;
569
        }
570
        /** Normal USB-MIDI protocol begins here. */
571
 
572
        if ( c > 0xf7 ) {       /* system: Realtime messages */
573
                /** Realtime messages are written IMMEDIATELY. */
574
                sysrt_buf[0] = (m->mout.cableId<<4) | 0x0f;
575
                sysrt_buf[1] = c;
576
                sysrt_buf[2] = 0;
577
                sysrt_buf[3] = 0;
578
                spin_lock_irqsave( &ep->lock, flags );
579
                ret = usb_write( ep, sysrt_buf, 4 );
580
                spin_unlock_irqrestore( &ep->lock, flags );
581
                /* m->mout.lastEvent = 0; */
582
 
583
                return ret;
584
        }
585
 
586
        if ( c >= 0x80 ) {
587
                if ( c < 0xf0 ) {
588
                        m->mout.lastEvent = c;
589
                        m->mout.isInExclusive = 0;
590
                        m->mout.bufRemains = get_remains(c);
591
                } else if ( c == 0xf0 ) {
592
                        /* m->mout.lastEvent = 0; */
593
                        m->mout.isInExclusive = 1;
594
                        m->mout.bufRemains = get_remains(c);
595
                } else if ( c == 0xf7 && m->mout.isInExclusive == 1 ) {
596
                        /* m->mout.lastEvent = 0; */
597
                        m->mout.isInExclusive = 0;
598
                        m->mout.bufRemains = 1;
599
                } else if ( c > 0xf0 ) {
600
                        /* m->mout.lastEvent = 0; */
601
                        m->mout.isInExclusive = 0;
602
                        m->mout.bufRemains = get_remains(c);
603
                }
604
 
605
        } else if ( m->mout.bufRemains == 0 && m->mout.isInExclusive == 0 ) {
606
                if ( m->mout.lastEvent == 0 ) {
607
                        return 0; /* discard, waiting for the first event */
608
                }
609
                /** track status **/
610
                m->mout.buf[0] = m->mout.lastEvent;
611
                m->mout.bufPtr = 1;
612
                m->mout.bufRemains = get_remains(m->mout.lastEvent)-1;
613
        }
614
 
615
        m->mout.buf[m->mout.bufPtr++] = c;
616
        m->mout.bufRemains--;
617
        if ( m->mout.bufRemains == 0 || m->mout.bufPtr >= 3) {
618
                ret = put_one_midi_event(m);
619
        }
620
 
621
        return ret;
622
}
623
 
624
 
625
/* ------------------------------------------------------------------------- */
626
 
627
/** Basic operation on /dev/midiXX as registered through struct file_operations.
628
 *
629
 *  Basic contract: Used to change the current read/write position in a file.
630
 *  On success, the non-negative position is reported.
631
 *  On failure, the negative of an error code is reported.
632
 *
633
 *  Because a MIDIStream is not a file, all seek operations are doomed to fail.
634
 *
635
 **/
636
static loff_t usb_midi_llseek(struct file *file, loff_t offset, int origin)
637
{
638
        /** Tell user you cannot seek on a PIPE-like device. **/
639
        return -ESPIPE;
640
}
641
 
642
 
643
/** Basic operation on /dev/midiXX as registered through struct file_operations.
644
 *
645
 * Basic contract: Block until count bytes have been read or an error occurs.
646
 *
647
 **/
648
 
649
static ssize_t usb_midi_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
650
{
651
        struct usb_mididev *m = (struct usb_mididev *)file->private_data;
652
        struct midi_in_endpoint *ep = m->min.ep;
653
        ssize_t ret;
654
        DECLARE_WAITQUEUE(wait, current);
655
 
656
        if ( ppos != &file->f_pos ) {
657
                return -ESPIPE;
658
        }
659
        if ( !access_ok(VERIFY_READ, buffer, count) ) {
660
                return -EFAULT;
661
        }
662
        if ( count == 0 ) {
663
                return 0;
664
        }
665
 
666
        add_wait_queue( &ep->wait, &wait );
667
        ret = 0;
668
        while( count > 0 ) {
669
                int cnt;
670
                int d = (int)count;
671
 
672
                cnt = m->min.bufRemains;
673
                if ( cnt > d ) {
674
                        cnt = d;
675
                }
676
 
677
                if ( cnt <= 0 ) {
678
                        if ( file->f_flags & O_NONBLOCK ) {
679
                                if (!ret)
680
                                        ret = -EAGAIN;
681
                                break;
682
                        }
683
                        __set_current_state(TASK_INTERRUPTIBLE);
684
                        schedule();
685
                        if (signal_pending(current)) {
686
                                if(!ret)
687
                                        ret=-ERESTARTSYS;
688
                                break;
689
                        }
690
                        continue;
691
                }
692
 
693
                {
694
                        int i;
695
                        unsigned long flags; /* used to synchronize access to the endpoint */
696
                        spin_lock_irqsave( &ep->lock, flags );
697
                        for (i = 0; i < cnt; i++) {
698
                                if ( copy_to_user( buffer+i, m->min.buf+m->min.bufRdPtr, 1 ) ) {
699
                                        if ( !ret )
700
                                                ret = -EFAULT;
701
                                        break;
702
                                }
703
                                m->min.bufRdPtr = (m->min.bufRdPtr+1)%MIDI_IN_BUFSIZ;
704
                                m->min.bufRemains -= 1;
705
                        }
706
                        spin_unlock_irqrestore( &ep->lock, flags );
707
                }
708
 
709
                count-=cnt;
710
                buffer+=cnt;
711
                ret+=cnt;
712
 
713
                break;
714
        }
715
 
716
        remove_wait_queue( &ep->wait, &wait );
717
        set_current_state(TASK_RUNNING);
718
 
719
        return ret;
720
}
721
 
722
 
723
/** Basic operation on /dev/midiXX as registered through struct file_operations.
724
 *
725
 *  Basic Contract: Take MIDI data byte-by-byte and pass it to
726
 *  writeMidi() which packages MIDI data into USB-MIDI stream.
727
 *  Then flushMidiData() is called to ensure all bytes have been written
728
 *  in a timely fashion.
729
 *
730
 **/
731
 
732
static ssize_t usb_midi_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
733
{
734
        struct usb_mididev *m = (struct usb_mididev *)file->private_data;
735
        ssize_t ret;
736
        unsigned long int flags;
737
 
738
        if ( ppos != &file->f_pos ) {
739
                return -ESPIPE;
740
        }
741
        if ( !access_ok(VERIFY_READ, buffer, count) ) {
742
                return -EFAULT;
743
        }
744
        if ( count == 0 ) {
745
                return 0;
746
        }
747
 
748
        ret = 0;
749
        while( count > 0 ) {
750
                unsigned char c;
751
 
752
                if (copy_from_user((unsigned char *)&c, buffer, 1)) {
753
                        if ( ret == 0 )
754
                                ret = -EFAULT;
755
                        break;
756
                }
757
                if( midi_write(m, (int)c) ) {
758
                        if ( ret == 0 )
759
                                ret = -EFAULT;
760
                        break;
761
                }
762
                count--;
763
                buffer++;
764
                ret++;
765
        }
766
 
767
        spin_lock_irqsave( &m->mout.ep->lock, flags );
768
        if ( flush_midi_buffer(m->mout.ep) < 0 ) {
769
                ret = -EFAULT;
770
        }
771
        spin_unlock_irqrestore( &m->mout.ep->lock, flags );
772
 
773
        return ret;
774
}
775
 
776
/** Basic operation on /dev/midiXX as registered through struct file_operations.
777
 *
778
 * Basic contract:  Wait (spin) until ready to read or write on the file.
779
 *
780
 **/
781
static unsigned int usb_midi_poll(struct file *file, struct poll_table_struct *wait)
782
{
783
        struct usb_mididev *m = (struct usb_mididev *)file->private_data;
784
        struct midi_in_endpoint *iep = m->min.ep;
785
        struct midi_out_endpoint *oep = m->mout.ep;
786
        unsigned long flags;
787
        unsigned int mask = 0;
788
 
789
        if ( file->f_mode & FMODE_READ ) {
790
                poll_wait( file, &iep->wait, wait );
791
                spin_lock_irqsave( &iep->lock, flags );
792
                if ( m->min.bufRemains > 0 )
793
                        mask |= POLLIN | POLLRDNORM;
794
                spin_unlock_irqrestore( &iep->lock, flags );
795
        }
796
 
797
        if ( file->f_mode & FMODE_WRITE ) {
798
                poll_wait( file, &oep->wait, wait );
799
                spin_lock_irqsave( &oep->lock, flags );
800
                if ( oep->bufWrPtr < oep->bufSize )
801
                        mask |= POLLOUT | POLLWRNORM;
802
                spin_unlock_irqrestore( &oep->lock, flags );
803
        }
804
 
805
        return mask;
806
}
807
 
808
 
809
/** Basic operation on /dev/midiXX as registered through struct file_operations.
810
 *
811
 * Basic contract: This is always the first operation performed on the
812
 * device node. If no method is defined, the open succeeds without any
813
 * notification given to the module.
814
 *
815
 **/
816
 
817
static int usb_midi_open(struct inode *inode, struct file *file)
818
{
819
        int minor = MINOR(inode->i_rdev);
820
        DECLARE_WAITQUEUE(wait, current);
821
        struct list_head      *devs, *mdevs;
822
        struct usb_midi_state *s;
823
        struct usb_mididev    *m;
824
        int flags;
825
        int succeed = 0;
826
 
827
#if 0
828
        printk(KERN_INFO "usb-midi: Open minor= %d.\n", minor);
829
#endif
830
 
831
        for(;;) {
832
                down(&open_sem);
833
                for (devs = mididevs.next; devs != &mididevs; devs = devs->next) {
834
                        s = list_entry(devs, struct usb_midi_state, mididev);
835
                        for (mdevs = s->midiDevList.next; mdevs != &s->midiDevList; mdevs = mdevs->next) {
836
                                m = list_entry(mdevs, struct usb_mididev, list);
837
                                if ( !((m->dev_midi ^ minor) & ~0xf) )
838
                                        goto device_found;
839
                        }
840
                }
841
                up(&open_sem);
842
                return -ENODEV;
843
 
844
        device_found:
845
                if ( !s->usbdev ) {
846
                        up(&open_sem);
847
                        return -EIO;
848
                }
849
                if ( !(m->open_mode & file->f_mode) ) {
850
                        break;
851
                }
852
                if ( file->f_flags & O_NONBLOCK ) {
853
                        up(&open_sem);
854
                        return -EBUSY;
855
                }
856
                __set_current_state(TASK_INTERRUPTIBLE);
857
                add_wait_queue( &open_wait, &wait );
858
                up(&open_sem);
859
                schedule();
860
                __set_current_state(TASK_RUNNING);
861
                remove_wait_queue( &open_wait, &wait );
862
                if ( signal_pending(current) ) {
863
                        return -ERESTARTSYS;
864
                }
865
        }
866
 
867
        file->private_data = m;
868
        spin_lock_irqsave( &s->lock, flags );
869
 
870
        if ( !(m->open_mode & (FMODE_READ | FMODE_WRITE)) ) {
871
                //FIXME: intented semantics unclear here
872
                m->min.bufRdPtr       = 0;
873
                m->min.bufWrPtr       = 0;
874
                m->min.bufRemains     = 0;
875
                spin_lock_init(&m->min.ep->lock);
876
 
877
                m->mout.bufPtr        = 0;
878
                m->mout.bufRemains    = 0;
879
                m->mout.isInExclusive = 0;
880
                m->mout.lastEvent     = 0;
881
                spin_lock_init(&m->mout.ep->lock);
882
        }
883
 
884
        if ( (file->f_mode & FMODE_READ) && m->min.ep != NULL ) {
885
                unsigned long int flagsep;
886
                spin_lock_irqsave( &m->min.ep->lock, flagsep );
887
                m->min.ep->cables[m->min.cableId] = m;
888
                m->min.ep->readers += 1;
889
                m->min.bufRdPtr       = 0;
890
                m->min.bufWrPtr       = 0;
891
                m->min.bufRemains     = 0;
892
                spin_unlock_irqrestore( &m->min.ep->lock, flagsep );
893
 
894
                if ( !(m->min.ep->urbSubmitted)) {
895
 
896
                        /* urb->dev must be reinitialized on 2.4.x kernels */
897
                        m->min.ep->urb->dev = m->min.ep->usbdev;
898
 
899
                        if ( usb_submit_urb(m->min.ep->urb) ) {
900
                                printk(KERN_ERR "usbmidi: Cannot submit urb for MIDI-IN\n");
901
                        }
902
                        m->min.ep->urbSubmitted = 1;
903
                }
904
                m->open_mode |= FMODE_READ;
905
                succeed = 1;
906
        }
907
 
908
        if ( (file->f_mode & FMODE_WRITE) && m->mout.ep != NULL ) {
909
                m->mout.bufPtr        = 0;
910
                m->mout.bufRemains    = 0;
911
                m->mout.isInExclusive = 0;
912
                m->mout.lastEvent     = 0;
913
                m->open_mode |= FMODE_WRITE;
914
                succeed = 1;
915
        }
916
 
917
        spin_unlock_irqrestore( &s->lock, flags );
918
 
919
        s->count++;
920
        up(&open_sem);
921
 
922
        /** Changed to prevent extra increments to USE_COUNT. **/
923
        if (!succeed) {
924
                return -EBUSY;
925
        }
926
 
927
#if 0
928
        printk(KERN_INFO "usb-midi: Open Succeeded. minor= %d.\n", minor);
929
#endif
930
 
931
        /** Side-effect: module cannot be removed until USE_COUNT is 0. **/
932
#ifndef MOD_INC_EACH_PROBE
933
        MOD_INC_USE_COUNT;
934
#endif
935
 
936
        return 0; /** Success. **/
937
}
938
 
939
 
940
/** Basic operation on /dev/midiXX as registered through struct file_operations.
941
 *
942
 *  Basic contract: Close an opened file and deallocate anything we allocated.
943
 *  Like open(), this can be missing. If open set file->private_data,
944
 *  release() must clear it.
945
 *
946
 **/
947
 
948
static int usb_midi_release(struct inode *inode, struct file *file)
949
{
950
        struct usb_mididev *m = (struct usb_mididev *)file->private_data;
951
        struct usb_midi_state *s = (struct usb_midi_state *)m->midi;
952
 
953
#if 0
954
        printk(KERN_INFO "usb-midi: Close.\n");
955
#endif
956
 
957
        down(&open_sem);
958
 
959
        if ( m->open_mode & FMODE_WRITE ) {
960
                m->open_mode &= ~FMODE_WRITE;
961
                usb_unlink_urb( m->mout.ep->urb );
962
        }
963
 
964
        if ( m->open_mode & FMODE_READ ) {
965
                unsigned long int flagsep;
966
                spin_lock_irqsave( &m->min.ep->lock, flagsep );
967
                m->min.ep->cables[m->min.cableId] = 0; // discard cable
968
                m->min.ep->readers -= 1;
969
                m->open_mode &= ~FMODE_READ;
970
                if ( m->min.ep->readers == 0 &&
971
                     m->min.ep->urbSubmitted ) {
972
                        m->min.ep->urbSubmitted = 0;
973
                        usb_unlink_urb(m->min.ep->urb);
974
                }
975
                spin_unlock_irqrestore( &m->min.ep->lock, flagsep );
976
        }
977
 
978
        s->count--;
979
 
980
        up(&open_sem);
981
        wake_up(&open_wait);
982
 
983
        file->private_data = 0;
984
        /** Sideeffect: Module cannot be removed until usecount is 0. */
985
#ifndef MOD_INC_EACH_PROBE
986
        MOD_DEC_USE_COUNT;
987
#endif
988
 
989
        return 0;
990
}
991
 
992
static struct file_operations usb_midi_fops = {
993
        llseek:         usb_midi_llseek,
994
        read:           usb_midi_read,
995
        write:          usb_midi_write,
996
        poll:           usb_midi_poll,
997
        open:           usb_midi_open,
998
        release:        usb_midi_release,
999
};
1000
 
1001
/* ------------------------------------------------------------------------- */
1002
 
1003
/** Returns filled midi_in_endpoint structure or null on failure.
1004
 *
1005
 * Parameters:
1006
 *      d        - a usb_device
1007
 *      endPoint - An usb endpoint in the range 0 to 15.
1008
 * Called by allocUsbMidiDev();
1009
 *
1010
 **/
1011
 
1012
static struct midi_in_endpoint *alloc_midi_in_endpoint( struct usb_device *d, int endPoint )
1013
{
1014
        struct midi_in_endpoint *ep;
1015
        int bufSize;
1016
        int pipe;
1017
 
1018
        endPoint &= 0x0f; /* Silently force endPoint to lie in range 0 to 15. */
1019
 
1020
        pipe =  usb_rcvbulkpipe( d, endPoint );
1021
        bufSize = usb_maxpacket( d, pipe, usb_pipein(pipe) );
1022
        /* usb_pipein() = ! usb_pipeout() = true for an in Endpoint */
1023
 
1024
        ep = (struct midi_in_endpoint *)kmalloc(sizeof(struct midi_in_endpoint), GFP_KERNEL);
1025
        if ( !ep ) {
1026
                printk(KERN_ERR "usbmidi: no memory for midi in-endpoint\n");
1027
                return NULL;
1028
        }
1029
        memset( ep, 0, sizeof(struct midi_in_endpoint) );
1030
//      this sets cables[] and readers to 0, too.
1031
//      for (i=0; i<16; i++) ep->cables[i] = 0; // discard cable
1032
//      ep->readers = 0;
1033
 
1034
        ep->endpoint = endPoint;
1035
 
1036
        ep->recvBuf = (unsigned char *)kmalloc(sizeof(unsigned char)*(bufSize), GFP_KERNEL);
1037
        if ( !ep->recvBuf ) {
1038
                printk(KERN_ERR "usbmidi: no memory for midi in-endpoint buffer\n");
1039
                kfree(ep);
1040
                return NULL;
1041
        }
1042
 
1043
        ep->urb = usb_alloc_urb(0); /* no ISO */
1044
        if ( !ep->urb ) {
1045
                printk(KERN_ERR "usbmidi: no memory for midi in-endpoint urb\n");
1046
                kfree(ep->recvBuf);
1047
                kfree(ep);
1048
                return NULL;
1049
        }
1050
        FILL_BULK_URB( ep->urb, d,
1051
                       usb_rcvbulkpipe(d, endPoint),
1052
                       (unsigned char *)ep->recvBuf, bufSize,
1053
                       (usb_complete_t)usb_bulk_read, ep );
1054
 
1055
        /* ep->bufRdPtr     = 0; */
1056
        /* ep->bufWrPtr     = 0; */
1057
        /* ep->bufRemains   = 0; */
1058
        /* ep->urbSubmitted = 0; */
1059
        ep->recvBufSize  = bufSize;
1060
 
1061
        init_waitqueue_head(&ep->wait);
1062
 
1063
        return ep;
1064
}
1065
 
1066
static int remove_midi_in_endpoint( struct midi_in_endpoint *min )
1067
{
1068
        usb_unlink_urb( min->urb );
1069
        usb_free_urb( min->urb );
1070
        kfree( min->recvBuf );
1071
        kfree( min );
1072
 
1073
        return 0;
1074
}
1075
 
1076
/** Returns filled midi_out_endpoint structure or null on failure.
1077
 *
1078
 * Parameters:
1079
 *      d        - a usb_device
1080
 *      endPoint - An usb endpoint in the range 0 to 15.
1081
 * Called by allocUsbMidiDev();
1082
 *
1083
 **/
1084
static struct midi_out_endpoint *alloc_midi_out_endpoint( struct usb_device *d, int endPoint )
1085
{
1086
        struct midi_out_endpoint *ep = NULL;
1087
        int pipe;
1088
        int bufSize;
1089
 
1090
        endPoint &= 0x0f;
1091
        pipe =  usb_sndbulkpipe( d, endPoint );
1092
        bufSize = usb_maxpacket( d, pipe, usb_pipeout(pipe) );
1093
 
1094
        ep = (struct midi_out_endpoint *)kmalloc(sizeof(struct midi_out_endpoint), GFP_KERNEL);
1095
        if ( !ep ) {
1096
                printk(KERN_ERR "usbmidi: no memory for midi out-endpoint\n");
1097
                return NULL;
1098
        }
1099
        memset( ep, 0, sizeof(struct midi_out_endpoint) );
1100
 
1101
        ep->endpoint = endPoint;
1102
        ep->buf = (unsigned char *)kmalloc(sizeof(unsigned char)*bufSize, GFP_KERNEL);
1103
        if ( !ep->buf ) {
1104
                printk(KERN_ERR "usbmidi: no memory for midi out-endpoint buffer\n");
1105
                kfree(ep);
1106
                return NULL;
1107
        }
1108
 
1109
        ep->urb = usb_alloc_urb(0); /* no ISO */
1110
        if ( !ep->urb ) {
1111
                printk(KERN_ERR "usbmidi: no memory for midi out-endpoint urb\n");
1112
                kfree(ep->buf);
1113
                kfree(ep);
1114
                return NULL;
1115
        }
1116
 
1117
        ep->bufSize       = bufSize;
1118
        /* ep->bufWrPtr      = 0; */
1119
 
1120
        init_waitqueue_head(&ep->wait);
1121
 
1122
        return ep;
1123
}
1124
 
1125
 
1126
static int remove_midi_out_endpoint( struct midi_out_endpoint *mout )
1127
{
1128
        usb_unlink_urb( mout->urb );
1129
        usb_free_urb( mout->urb );
1130
        kfree( mout->buf );
1131
        kfree( mout );
1132
 
1133
        return 0;
1134
}
1135
 
1136
 
1137
/** Returns a filled usb_mididev structure, registered as a Linux MIDI device.
1138
 *
1139
 * Returns null if memory is not available or the device cannot be registered.
1140
 * Called by allocUsbMidiDev();
1141
 *
1142
 **/
1143
static struct usb_mididev *allocMidiDev(
1144
        struct usb_midi_state *s,
1145
        struct midi_in_endpoint *min,
1146
        struct midi_out_endpoint *mout,
1147
        int inCableId,
1148
        int outCableId )
1149
{
1150
        struct usb_mididev *m;
1151
 
1152
        m = (struct usb_mididev *)kmalloc(sizeof(struct usb_mididev), GFP_KERNEL);
1153
        if (!m) {
1154
                printk(KERN_ERR "usbmidi: no memory for midi device\n");
1155
                return NULL;
1156
        }
1157
 
1158
        memset(m, 0, sizeof(struct usb_mididev));
1159
 
1160
        if ((m->dev_midi = register_sound_midi(&usb_midi_fops, -1)) < 0) {
1161
                printk(KERN_ERR "usbmidi: cannot register midi device\n");
1162
                kfree(m);
1163
                return NULL;
1164
        }
1165
 
1166
        m->midi               = s;
1167
        /* m->open_mode          = 0; */
1168
 
1169
        if ( min ) {
1170
                m->min.ep             = min;
1171
                m->min.ep->usbdev     = s->usbdev;
1172
                m->min.cableId        = inCableId;
1173
        }
1174
        /* m->min.bufPtr         = 0; */
1175
        /* m->min.bufRemains     = 0; */
1176
 
1177
        if ( mout ) {
1178
                m->mout.ep            = mout;
1179
                m->mout.ep->usbdev    = s->usbdev;
1180
                m->mout.cableId       = outCableId;
1181
        }
1182
        /* m->mout.bufPtr        = 0; */
1183
        /* m->mout.bufRemains    = 0; */
1184
        /* m->mout.isInExclusive = 0; */
1185
        /* m->mout.lastEvent     = 0; */
1186
 
1187
        m->singlebyte         = singlebyte;
1188
 
1189
        return m;
1190
}
1191
 
1192
 
1193
static void release_midi_device( struct usb_midi_state *s )
1194
{
1195
        struct usb_mididev *m;
1196
        struct midi_in_endpoint *min;
1197
        struct midi_out_endpoint *mout;
1198
 
1199
        if ( s->count > 0 ) {
1200
                up(&open_sem);
1201
                return;
1202
        }
1203
        up( &open_sem );
1204
        wake_up( &open_wait );
1205
 
1206
        while(!list_empty(&s->inEndpointList)) {
1207
                min = list_entry(s->inEndpointList.next, struct midi_in_endpoint, list);
1208
                list_del(&min->list);
1209
                remove_midi_in_endpoint(min);
1210
        }
1211
 
1212
        while(!list_empty(&s->outEndpointList)) {
1213
                mout = list_entry(s->outEndpointList.next, struct midi_out_endpoint, list);
1214
                list_del(&mout->list);
1215
                remove_midi_out_endpoint(mout);
1216
        }
1217
 
1218
        while(!list_empty(&s->midiDevList)) {
1219
                m = list_entry(s->midiDevList.next, struct usb_mididev, list);
1220
                list_del(&m->list);
1221
                kfree(m);
1222
        }
1223
 
1224
        kfree(s);
1225
 
1226
        return;
1227
}
1228
 
1229
 
1230
/* ------------------------------------------------------------------------- */
1231
 
1232
/** Utility routine to find a descriptor in a dump of many descriptors.
1233
 * Returns start of descriptor or NULL if not found.
1234
 * descStart pointer to list of interfaces.
1235
 * descLength length (in bytes) of dump
1236
 * after (ignored if NULL) this routine returns only descriptors after "after"
1237
 * dtype (mandatory) The descriptor type.
1238
 * iface (ignored if -1) returns descriptor at/following given interface
1239
 * altSetting (ignored if -1) returns descriptor at/following given altSetting
1240
 *
1241
 *
1242
 *  Called by parseDescriptor(), find_csinterface_descriptor();
1243
 *
1244
 */
1245
static void *find_descriptor( void *descStart, unsigned int descLength, void *after, unsigned char dtype, int iface, int altSetting )
1246
{
1247
        unsigned char *p, *end, *next;
1248
        int interfaceNumber = -1, altSet = -1;
1249
 
1250
        p = descStart;
1251
        end = p + descLength;
1252
        for( ; p < end; ) {
1253
                if ( p[0] < 2 )
1254
                        return NULL;
1255
                next = p + p[0];
1256
                if ( next > end )
1257
                        return NULL;
1258
                if ( p[1] == USB_DT_INTERFACE ) {
1259
                        if ( p[0] < USB_DT_INTERFACE_SIZE )
1260
                                return NULL;
1261
                        interfaceNumber = p[2];
1262
                        altSet = p[3];
1263
                }
1264
                if ( p[1] == dtype &&
1265
                     ( !after || ( p > (unsigned char *)after) ) &&
1266
                     ( ( iface == -1) || (iface == interfaceNumber) ) &&
1267
                     ( (altSetting == -1) || (altSetting == altSet) )) {
1268
                        return p;
1269
                }
1270
                p = next;
1271
        }
1272
        return NULL;
1273
}
1274
 
1275
/** Utility to find a class-specfic interface descriptor.
1276
 *  dsubtype is a descriptor subtype
1277
 *  Called by parseDescriptor();
1278
 **/
1279
static void *find_csinterface_descriptor(void *descStart, unsigned int descLength, void *after, u8 dsubtype, int iface, int altSetting)
1280
{
1281
        unsigned char *p;
1282
 
1283
        p = find_descriptor( descStart, descLength, after, USB_DT_CS_INTERFACE, iface, altSetting );
1284
        while ( p ) {
1285
                if ( p[0] >= 3 && p[2] == dsubtype )
1286
                        return p;
1287
                p = find_descriptor( descStart, descLength, p, USB_DT_CS_INTERFACE,
1288
                                     iface, altSetting );
1289
        }
1290
        return NULL;
1291
}
1292
 
1293
 
1294
/** The magic of making a new usb_midi_device from config happens here.
1295
 *
1296
 * The caller is responsible for free-ing this return value (if not NULL).
1297
 *
1298
 **/
1299
static struct usb_midi_device *parse_descriptor( struct usb_device *d, unsigned char *buffer, int bufSize, unsigned int ifnum , unsigned int altSetting, int quirks)
1300
{
1301
        struct usb_midi_device *u;
1302
        unsigned char *p1;
1303
        unsigned char *p2;
1304
        unsigned char *next;
1305
        int iep, oep;
1306
        int length;
1307
        unsigned long longBits;
1308
        int pins, nbytes, offset, shift, jack;
1309
#ifdef HAVE_JACK_STRINGS
1310
        /** Jacks can have associated names.  **/
1311
        unsigned char jack2string[256];
1312
#endif
1313
 
1314
        u = 0;
1315
        /* find audiocontrol interface */
1316
        p1 = find_csinterface_descriptor( buffer, bufSize, NULL,
1317
                                          MS_HEADER, ifnum, altSetting);
1318
 
1319
        if ( !p1 ) {
1320
                goto error_end;
1321
        }
1322
 
1323
        if ( p1[0] < MS_HEADER_LENGTH ) {
1324
                goto error_end;
1325
        }
1326
 
1327
        /* Assume success. Since the device corresponds to USB-MIDI spec, we assume
1328
           that the rest of the USB 2.0 spec is obeyed. */
1329
 
1330
        u = (struct usb_midi_device *)kmalloc( sizeof(struct usb_midi_device), GFP_KERNEL );
1331
        if ( !u ) {
1332
                return NULL;
1333
        }
1334
        u->deviceName = 0;
1335
        u->idVendor = d->descriptor.idVendor;
1336
        u->idProduct = d->descriptor.idProduct;
1337
        u->interface = ifnum;
1338
        u->altSetting = altSetting;
1339
        u->in[0].endpoint = -1;
1340
        u->in[0].cableId = -1;
1341
        u->out[0].endpoint = -1;
1342
        u->out[0].cableId = -1;
1343
 
1344
 
1345
        printk(KERN_INFO "usb-midi: Found MIDIStreaming device corresponding to Release %d.%02d of spec.\n",
1346
               (p1[4] >> 4) * 10 + (p1[4] & 0x0f ),
1347
               (p1[3] >> 4) * 10 + (p1[3] & 0x0f )
1348
                );
1349
 
1350
        length = p1[5] | (p1[6] << 8);
1351
 
1352
#ifdef HAVE_JACK_STRINGS
1353
        memset(jack2string, 0, sizeof(unsigned char) * 256);
1354
#endif
1355
 
1356
        length -= p1[0];
1357
        for (p2 = p1 + p1[0]; length > 0; p2 = next) {
1358
                next = p2 + p2[0];
1359
                length -= p2[0];
1360
 
1361
                if (p2[0] < 2 ) break;
1362
                if (p2[1] != USB_DT_CS_INTERFACE) break;
1363
                if (p2[2] == MIDI_IN_JACK && p2[0] >= 6 ) {
1364
                        jack = p2[4];
1365
#ifdef HAVE_JACK_STRINGS
1366
                        jack2string[jack] = p2[5];
1367
#endif
1368
                        printk(KERN_INFO "usb-midi: Found IN Jack 0x%02x %s\n",
1369
                               jack, (p2[3] == EMBEDDED_JACK)?"EMBEDDED":"EXTERNAL" );
1370
                } else if ( p2[2] == MIDI_OUT_JACK && p2[0] >= 6) {
1371
                        pins = p2[5];
1372
                        if ( p2[0] < (6 + 2 * pins) ) continue;
1373
                        jack = p2[4];
1374
#ifdef HAVE_JACK_STRINGS
1375
                        jack2string[jack] = p2[5 + 2 * pins];
1376
#endif
1377
                        printk(KERN_INFO "usb-midi: Found OUT Jack 0x%02x %s, %d pins\n",
1378
                               jack, (p2[3] == EMBEDDED_JACK)?"EMBEDDED":"EXTERNAL", pins );
1379
                } else if ( p2[2] == ELEMENT_DESCRIPTOR  && p2[0]  >= 10) {
1380
                        pins = p2[4];
1381
                        if ( p2[0] < (9 + 2 * pins ) ) continue;
1382
                        nbytes = p2[8 + 2 * pins ];
1383
                        if ( p2[0] < (10 + 2 * pins + nbytes) ) continue;
1384
                        longBits = 0L;
1385
                        for ( offset = 0, shift = 0; offset < nbytes && offset < 8; offset ++, shift += 8) {
1386
                                longBits |= ((long)(p2[9 + 2 * pins + offset])) << shift;
1387
                        }
1388
                        jack = p2[3];
1389
#ifdef HAVE_JACK_STRINGS
1390
                        jack2string[jack] = p2[9 + 2 * pins + nbytes];
1391
#endif
1392
                        printk(KERN_INFO "usb-midi: Found ELEMENT 0x%02x, %d/%d pins in/out, bits: 0x%016lx\n",
1393
                               jack, pins, (int)(p2[5 + 2 * pins]), (long)longBits );
1394
                } else {
1395
                }
1396
        }
1397
 
1398
        iep=0;
1399
        oep=0;
1400
 
1401
        if (quirks==0) {
1402
                /* MIDISTREAM */
1403
                p2 = 0;
1404
                for (p1 = find_descriptor(buffer, bufSize, NULL, USB_DT_ENDPOINT,
1405
                                          ifnum, altSetting ); p1; p1 = next ) {
1406
                        next = find_descriptor(buffer, bufSize, p1, USB_DT_ENDPOINT,
1407
                                               ifnum, altSetting );
1408
                        p2 = find_descriptor(buffer, bufSize, p1, USB_DT_CS_ENDPOINT,
1409
                                             ifnum, altSetting );
1410
 
1411
                        if ( p2 && next && ( p2 > next ) )
1412
                                p2 = 0;
1413
 
1414
                        if ( p1[0] < 9 || !p2 || p2[0] < 4 ) continue;
1415
 
1416
                        if ( (p1[2] & 0x80) == 0x80 ) {
1417
                                if ( iep < 15 ) {
1418
                                        pins = p2[3]; /* not pins -- actually "cables" */
1419
                                        if ( pins > 16 )
1420
                                                pins = 16;
1421
                                        u->in[iep].endpoint = p1[2];
1422
                                        u->in[iep].cableId = ( 1 << pins ) - 1;
1423
                                        if ( u->in[iep].cableId ) iep ++;
1424
                                        if ( iep < 15 ) {
1425
                                                u->in[iep].endpoint = -1;
1426
                                                u->in[iep].cableId = -1;
1427
                                        }
1428
                                }
1429
                        } else {
1430
                                if ( oep < 15 ) {
1431
                                        pins = p2[3]; /* not pins -- actually "cables" */
1432
                                        if ( pins > 16 )
1433
                                                pins = 16;
1434
                                        u->out[oep].endpoint = p1[2];
1435
                                        u->out[oep].cableId = ( 1 << pins ) - 1;
1436
                                        if ( u->out[oep].cableId ) oep ++;
1437
                                        if ( oep < 15 ) {
1438
                                                u->out[oep].endpoint = -1;
1439
                                                u->out[oep].cableId = -1;
1440
                                        }
1441
                                }
1442
                        }
1443
 
1444
                }
1445
        } else if (quirks==1) {
1446
                /* YAMAHA quirks */
1447
                for (p1 = find_descriptor(buffer, bufSize, NULL, USB_DT_ENDPOINT,
1448
                                          ifnum, altSetting ); p1; p1 = next ) {
1449
                        next = find_descriptor(buffer, bufSize, p1, USB_DT_ENDPOINT,
1450
                                               ifnum, altSetting );
1451
 
1452
                        if ( p1[0] < 7 ) continue;
1453
 
1454
                        if ( (p1[2] & 0x80) == 0x80 ) {
1455
                                if ( iep < 15 ) {
1456
                                        pins = iep+1;
1457
                                        if ( pins > 16 )
1458
                                                pins = 16;
1459
                                        u->in[iep].endpoint = p1[2];
1460
                                        u->in[iep].cableId = ( 1 << pins ) - 1;
1461
                                        if ( u->in[iep].cableId ) iep ++;
1462
                                        if ( iep < 15 ) {
1463
                                                u->in[iep].endpoint = -1;
1464
                                                u->in[iep].cableId = -1;
1465
                                        }
1466
                                }
1467
                        } else {
1468
                                if ( oep < 15 ) {
1469
                                        pins = oep+1;
1470
                                        if ( pins > 16 )
1471
                                                pins = 16;
1472
                                        u->out[oep].endpoint = p1[2];
1473
                                        u->out[oep].cableId = ( 1 << pins ) - 1;
1474
                                        if ( u->out[oep].cableId ) oep ++;
1475
                                        if ( oep < 15 ) {
1476
                                                u->out[oep].endpoint = -1;
1477
                                                u->out[oep].cableId = -1;
1478
                                        }
1479
                                }
1480
                        }
1481
 
1482
                }
1483
        }
1484
 
1485
        if ( !iep && ! oep ) {
1486
                goto error_end;
1487
        }
1488
 
1489
        return u;
1490
 
1491
error_end:
1492
        if ( u ) kfree(u);
1493
        return NULL;
1494
}
1495
 
1496
/* ------------------------------------------------------------------------- */
1497
 
1498
/** Returns number between 0 and 16.
1499
 *
1500
 **/
1501
static int on_bits( unsigned short v )
1502
{
1503
        int i;
1504
        int ret=0;
1505
 
1506
        for ( i=0 ; i<16 ; i++ ) {
1507
                if ( v & (1<<i) ) ret++;
1508
        }
1509
 
1510
        return ret;
1511
}
1512
 
1513
 
1514
/** USB-device will be interrogated for altSetting.
1515
 *
1516
 * Returns negative on error.
1517
 * Called by allocUsbMidiDev();
1518
 *
1519
 **/
1520
 
1521
static int get_alt_setting( struct usb_device *d, int ifnum )
1522
{
1523
        int alts, alt=0;
1524
        struct usb_interface_descriptor *interface;
1525
        struct usb_endpoint_descriptor *ep;
1526
        int epin, epout;
1527
        int i;
1528
 
1529
        alts = d->actconfig->interface[ifnum].num_altsetting;
1530
 
1531
        for ( alt=0 ; alt<alts ; alt++ ) {
1532
                interface = &d->actconfig->interface[ifnum].altsetting[alt];
1533
                epin = -1;
1534
                epout = -1;
1535
 
1536
                for ( i=0 ; i<interface->bNumEndpoints ; i++ ) {
1537
                        ep = &interface->endpoint[i];
1538
                        if ( (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ) {
1539
                                continue;
1540
                        }
1541
                        if ( (ep->bEndpointAddress & USB_DIR_IN) && epin < 0 ) {
1542
                                epin = i;
1543
                        } else if ( epout < 0 ) {
1544
                                epout = i;
1545
                        }
1546
                        if ( epin >= 0 && epout >= 0 ) {
1547
                                return alt;
1548
                        }
1549
                }
1550
        }
1551
 
1552
        return -ENODEV;
1553
}
1554
 
1555
 
1556
/* ------------------------------------------------------------------------- */
1557
 
1558
 
1559
/** Returns 0 if successful in allocating and registering internal structures.
1560
 * Returns negative on failure.
1561
 * Calls allocMidiDev which additionally registers /dev/midiXX devices.
1562
 * Writes messages on success to indicate which /dev/midiXX is which physical
1563
 * endpoint.
1564
 *
1565
 **/
1566
static int alloc_usb_midi_device( struct usb_device *d, struct usb_midi_state *s, struct usb_midi_device *u )
1567
{
1568
        struct usb_mididev **mdevs=NULL;
1569
        struct midi_in_endpoint *mins[15], *min;
1570
        struct midi_out_endpoint *mouts[15], *mout;
1571
        int inDevs=0, outDevs=0;
1572
        int inEndpoints=0, outEndpoints=0;
1573
        int inEndpoint, outEndpoint;
1574
        int inCableId, outCableId;
1575
        int i;
1576
        int devices = 0;
1577
        int alt = 0;
1578
 
1579
        /* Obtain altSetting or die.. */
1580
        alt = u->altSetting;
1581
        if ( alt < 0 ) {
1582
                alt = get_alt_setting( d, u->interface );
1583
        }
1584
        if ( alt < 0 ) { return -ENXIO; }
1585
 
1586
        /* Configure interface */
1587
        if ( usb_set_interface( d, u->interface, alt ) < 0 ) {
1588
                return -ENXIO;
1589
        }
1590
 
1591
        for ( i = 0 ; i < 15 ; i++ ) {
1592
                mins[i] = NULL;
1593
                mouts[i] = NULL;
1594
        }
1595
 
1596
        /* Begin Allocation */
1597
        while( inEndpoints < 15
1598
               && inDevs < maxdevices
1599
               && u->in[inEndpoints].cableId >= 0 ) {
1600
                inDevs += on_bits((unsigned short)u->in[inEndpoints].cableId);
1601
                mins[inEndpoints] = alloc_midi_in_endpoint( d, u->in[inEndpoints].endpoint );
1602
                if ( mins[inEndpoints] == NULL ) { goto error_end; }
1603
                inEndpoints++;
1604
        }
1605
 
1606
        while( outEndpoints < 15
1607
               && outDevs < maxdevices
1608
               && u->out[outEndpoints].cableId >= 0 ) {
1609
                outDevs += on_bits((unsigned short)u->out[outEndpoints].cableId);
1610
                mouts[outEndpoints] = alloc_midi_out_endpoint( d, u->out[outEndpoints].endpoint );
1611
                if ( mouts[outEndpoints] == NULL ) { goto error_end; }
1612
                outEndpoints++;
1613
        }
1614
 
1615
        devices = inDevs > outDevs ? inDevs : outDevs;
1616
        devices = maxdevices > devices ? devices : maxdevices;
1617
 
1618
        /* obtain space for device name (iProduct) if not known. */
1619
        if ( ! u->deviceName ) {
1620
                mdevs = (struct usb_mididev **)
1621
                        kmalloc(sizeof(struct usb_mididevs *)*devices
1622
                                + sizeof(char) * 256, GFP_KERNEL);
1623
        } else {
1624
                mdevs = (struct usb_mididev **)
1625
                        kmalloc(sizeof(struct usb_mididevs *)*devices, GFP_KERNEL);
1626
        }
1627
 
1628
        if ( !mdevs ) {
1629
                /* devices = 0; */
1630
                /* mdevs = NULL; */
1631
                goto error_end;
1632
        }
1633
        for ( i=0 ; i<devices ; i++ ) {
1634
                mdevs[i] = NULL;
1635
        }
1636
 
1637
        /* obtain device name (iProduct) if not known. */
1638
        if ( ! u->deviceName ) {
1639
                u->deviceName = (char *) (mdevs + devices);
1640
                if ( ! d->have_langid && d->descriptor.iProduct) {
1641
                        alt = usb_get_string(d, 0, 0, u->deviceName, 250);
1642
                        if (alt < 0) {
1643
                                printk(KERN_INFO "error getting string descriptor 0 (error=%d)\n", alt);
1644
                        } else if (u->deviceName[0] < 4) {
1645
                                printk(KERN_INFO "string descriptor 0 too short (length = %d)\n", alt);
1646
                        } else {
1647
                                printk(KERN_INFO "string descriptor 0 found (length = %d)\n", alt);
1648
                                for(; alt >= 4; alt -= 2) {
1649
                                        i = u->deviceName[alt-2] | (u->deviceName[alt-1]<< 8);
1650
                                        printk(KERN_INFO "usb-midi: langid(%d) 0x%04x\n",
1651
                                               (alt-4) >> 1, i);
1652
                                        if ( ( ( i ^ ulangid ) & 0xff ) == 0 ) {
1653
                                                d->have_langid = 1;
1654
                                                d->string_langid = i;
1655
                                                printk(KERN_INFO "usb-midi: langid(match) 0x%04x\n", i);
1656
                                                if ( i == ulangid )
1657
                                                        break;
1658
                                        }
1659
                                }
1660
                        }
1661
                }
1662
                u->deviceName[0] = (char) 0;
1663
                if (d->descriptor.iProduct) {
1664
                        printk(KERN_INFO "usb-midi: fetchString(%d)\n", d->descriptor.iProduct);
1665
                        alt = usb_string(d, d->descriptor.iProduct, u->deviceName, 255);
1666
                        if( alt < 0 ) {
1667
                                u->deviceName[0] = (char) 0;
1668
                        }
1669
                        printk(KERN_INFO "usb-midi: fetchString = %d\n", alt);
1670
                }
1671
                /* Failsafe */
1672
                if ( !u->deviceName[0] ) {
1673
                        if ( d->descriptor.idVendor == USB_VENDOR_ID_ROLAND ) {
1674
                                strcpy(u->deviceName, "Unknown Roland");
1675
                        } else if ( d->descriptor.idVendor == USB_VENDOR_ID_STEINBERG  ) {
1676
                                strcpy(u->deviceName, "Unknown Steinberg");
1677
                        } else if ( d->descriptor.idVendor == USB_VENDOR_ID_YAMAHA ) {
1678
                                strcpy(u->deviceName, "Unknown Yamaha");
1679
                        } else {
1680
                                strcpy(u->deviceName, "Unknown");
1681
                        }
1682
                }
1683
        }
1684
 
1685
        inEndpoint  = 0; inCableId  = -1;
1686
        outEndpoint = 0; outCableId = -1;
1687
 
1688
        for ( i=0 ; i<devices ; i++ ) {
1689
                for ( inCableId ++ ;
1690
                      inEndpoint <15
1691
                              && mins[inEndpoint]
1692
                              && !(u->in[inEndpoint].cableId & (1<<inCableId)) ;
1693
                      inCableId++ ) {
1694
                        if ( inCableId >= 16 ) {
1695
                                inEndpoint  ++;
1696
                                inCableId  = 0;
1697
                        }
1698
                }
1699
                min  = mins[inEndpoint];
1700
                for ( outCableId ++ ;
1701
                      outEndpoint <15
1702
                              && mouts[outEndpoint]
1703
                              && !(u->out[outEndpoint].cableId & (1<<outCableId)) ;
1704
                      outCableId++ ) {
1705
                        if ( outCableId >= 16 ) {
1706
                                outEndpoint  ++;
1707
                                outCableId  = 0;
1708
                        }
1709
                }
1710
                mout = mouts[outEndpoint];
1711
 
1712
                mdevs[i] = allocMidiDev( s, min, mout, inCableId, outCableId );
1713
                if ( mdevs[i] == NULL ) { goto error_end; }
1714
 
1715
        }
1716
 
1717
        /* Success! */
1718
        for ( i=0 ; i<devices ; i++ ) {
1719
                list_add_tail( &mdevs[i]->list, &s->midiDevList );
1720
        }
1721
        for ( i=0 ; i<inEndpoints ; i++ ) {
1722
                list_add_tail( &mins[i]->list, &s->inEndpointList );
1723
        }
1724
        for ( i=0 ; i<outEndpoints ; i++ ) {
1725
                list_add_tail( &mouts[i]->list, &s->outEndpointList );
1726
        }
1727
 
1728
        printk(KERN_INFO "usbmidi: found [ %s ] (0x%04x:0x%04x), attached:\n", u->deviceName, u->idVendor, u->idProduct );
1729
        for ( i=0 ; i<devices ; i++ ) {
1730
                int dm = (mdevs[i]->dev_midi-2)>>4;
1731
                if ( mdevs[i]->mout.ep != NULL && mdevs[i]->min.ep != NULL ) {
1732
                        printk(KERN_INFO "usbmidi: /dev/midi%02d: in (ep:%02x cid:%2d bufsiz:%2d) out (ep:%02x cid:%2d bufsiz:%2d)\n",
1733
                               dm,
1734
                               mdevs[i]->min.ep->endpoint|USB_DIR_IN, mdevs[i]->min.cableId, mdevs[i]->min.ep->recvBufSize,
1735
                               mdevs[i]->mout.ep->endpoint, mdevs[i]->mout.cableId, mdevs[i]->mout.ep->bufSize);
1736
                } else if ( mdevs[i]->min.ep != NULL ) {
1737
                        printk(KERN_INFO "usbmidi: /dev/midi%02d: in (ep:%02x cid:%2d bufsiz:%02d)\n",
1738
                               dm,
1739
                               mdevs[i]->min.ep->endpoint|USB_DIR_IN, mdevs[i]->min.cableId, mdevs[i]->min.ep->recvBufSize);
1740
                } else if ( mdevs[i]->mout.ep != NULL ) {
1741
                        printk(KERN_INFO "usbmidi: /dev/midi%02d: out (ep:%02x cid:%2d bufsiz:%02d)\n",
1742
                               dm,
1743
                               mdevs[i]->mout.ep->endpoint, mdevs[i]->mout.cableId, mdevs[i]->mout.ep->bufSize);
1744
                }
1745
        }
1746
 
1747
        kfree(mdevs);
1748
        return 0;
1749
 
1750
 error_end:
1751
        if ( mdevs != NULL && devices > 0 ) {
1752
                for ( i=0 ; i<devices ; i++ ) {
1753
                        if ( mdevs[i] != NULL ) {
1754
                                unregister_sound_midi( mdevs[i]->dev_midi );
1755
                                kfree(mdevs[i]);
1756
                        }
1757
                }
1758
                kfree(mdevs);
1759
        }
1760
 
1761
        for ( i=0 ; i<15 ; i++ ) {
1762
                if ( mins[i] != NULL ) {
1763
                        remove_midi_in_endpoint( mins[i] );
1764
                }
1765
                if ( mouts[i] != NULL ) {
1766
                        remove_midi_out_endpoint( mouts[i] );
1767
                }
1768
        }
1769
 
1770
        return -ENOMEM;
1771
}
1772
 
1773
/* ------------------------------------------------------------------------- */
1774
 
1775
/** Attempt to scan YAMAHA's device descriptor and detect correct values of
1776
 *  them.
1777
 *  Return 0 on succes, negative on failure.
1778
 *  Called by usb_midi_probe();
1779
 **/
1780
 
1781
static int detect_yamaha_device( struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s)
1782
{
1783
        struct usb_config_descriptor    *c = d->actconfig;
1784
        struct usb_interface_descriptor *interface;
1785
        struct usb_midi_device *u;
1786
        unsigned char buf[USB_DT_CONFIG_SIZE], *buffer;
1787
        int bufSize;
1788
        int i;
1789
        int alts=-1;
1790
        int ret;
1791
 
1792
        if (d->descriptor.idVendor != USB_VENDOR_ID_YAMAHA) {
1793
                return -EINVAL;
1794
        }
1795
 
1796
        for ( i=0 ; i < c->interface[ifnum].num_altsetting; i++ ) {
1797
                interface = c->interface[ifnum].altsetting + i;
1798
 
1799
                if ( interface->bInterfaceClass != 255 ||
1800
                     interface->bInterfaceSubClass != 0 )
1801
                        continue;
1802
                alts = i;
1803
        }
1804
        if ( alts == -1 ) {
1805
                return -EINVAL;
1806
        }
1807
 
1808
        printk(KERN_INFO "usb-midi: Found YAMAHA USB-MIDI device on dev %04x:%04x, iface %d\n",
1809
               d->descriptor.idVendor, d->descriptor.idProduct, ifnum);
1810
 
1811
        for ( i=0 ; i < d->descriptor.bNumConfigurations ; i++ ) {
1812
                if ( d->config+i == c ) goto configfound;
1813
        }
1814
 
1815
        printk(KERN_INFO "usb-midi: Config not found.\n");
1816
 
1817
        return -EINVAL;
1818
 
1819
 configfound:
1820
 
1821
        /* this may not be necessary. */
1822
        if ( usb_set_configuration( d, c->bConfigurationValue ) < 0 ) {
1823
                printk(KERN_INFO "usb-midi: Could not set config.\n");
1824
                return -EINVAL;
1825
        }
1826
 
1827
        ret = usb_get_descriptor( d, USB_DT_CONFIG, i, buf, USB_DT_CONFIG_SIZE );
1828
        if ( ret < 0 ) {
1829
                printk(KERN_INFO "usb-midi: Could not get config (error=%d).\n", ret);
1830
                return -EINVAL;
1831
        }
1832
        if ( buf[1] != USB_DT_CONFIG || buf[0] < USB_DT_CONFIG_SIZE ) {
1833
                printk(KERN_INFO "usb-midi: config not as expected.\n");
1834
                return -EINVAL;
1835
        }
1836
        bufSize = buf[2] | buf[3]<<8;
1837
        buffer = (unsigned char *)kmalloc(sizeof(unsigned char)*bufSize, GFP_KERNEL);
1838
        if ( !buffer ) {
1839
                printk(KERN_INFO "usb-midi: Could not allocate memory.\n");
1840
                return -EINVAL;
1841
        }
1842
        ret = usb_get_descriptor( d, USB_DT_CONFIG, i, buffer, bufSize );
1843
        if ( ret < 0 ) {
1844
                printk(KERN_INFO "usb-midi: Could not get full config (error=%d).\n", ret);
1845
                kfree(buffer);
1846
                return -EINVAL;
1847
        }
1848
 
1849
        u = parse_descriptor( d, buffer, bufSize, ifnum, alts, 1);
1850
        kfree(buffer);
1851
        if ( u == NULL ) {
1852
                return -EINVAL;
1853
        }
1854
 
1855
        ret = alloc_usb_midi_device( d, s, u );
1856
 
1857
        kfree(u);
1858
 
1859
        return ret;
1860
}
1861
 
1862
 
1863
/** Scan table of known devices which are only partially compliant with
1864
 * the MIDIStreaming specification.
1865
 * Called by usb_midi_probe();
1866
 *
1867
 **/
1868
 
1869
static int detect_vendor_specific_device( struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s )
1870
{
1871
        struct usb_midi_device *u;
1872
        int i;
1873
        int ret = -ENXIO;
1874
 
1875
        for ( i=0; i<VENDOR_SPECIFIC_USB_MIDI_DEVICES ; i++ ) {
1876
                u=&(usb_midi_devices[i]);
1877
 
1878
                if ( d->descriptor.idVendor != u->idVendor ||
1879
                     d->descriptor.idProduct != u->idProduct ||
1880
                     ifnum != u->interface )
1881
                        continue;
1882
 
1883
                ret = alloc_usb_midi_device( d, s, u );
1884
                break;
1885
        }
1886
 
1887
        return ret;
1888
}
1889
 
1890
 
1891
/** Attempt to match any config of an interface to a MIDISTREAMING interface.
1892
 *  Returns 0 on success, negative on failure.
1893
 * Called by usb_midi_probe();
1894
 **/
1895
static int detect_midi_subclass(struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s)
1896
{
1897
        struct usb_config_descriptor    *c = d->actconfig;
1898
        struct usb_interface_descriptor *interface;
1899
        struct usb_midi_device *u;
1900
        unsigned char buf[USB_DT_CONFIG_SIZE], *buffer;
1901
        int bufSize;
1902
        int i;
1903
        int alts=-1;
1904
        int ret;
1905
 
1906
        for ( i=0 ; i < c->interface[ifnum].num_altsetting; i++ ) {
1907
                interface = c->interface[ifnum].altsetting + i;
1908
 
1909
                if ( interface->bInterfaceClass != USB_CLASS_AUDIO ||
1910
                     interface->bInterfaceSubClass != USB_SUBCLASS_MIDISTREAMING )
1911
                        continue;
1912
                alts = i;
1913
        }
1914
        if ( alts == -1 ) {
1915
                return -EINVAL;
1916
        }
1917
 
1918
        printk(KERN_INFO "usb-midi: Found MIDISTREAMING on dev %04x:%04x, iface %d\n",
1919
               d->descriptor.idVendor, d->descriptor.idProduct, ifnum);
1920
 
1921
        for ( i=0 ; i < d->descriptor.bNumConfigurations ; i++ ) {
1922
                if ( d->config+i == c ) goto configfound;
1923
        }
1924
 
1925
        printk(KERN_INFO "usb-midi: Config not found.\n");
1926
 
1927
        return -EINVAL;
1928
 
1929
 configfound:
1930
 
1931
        /* this may not be necessary. */
1932
        if ( usb_set_configuration( d, c->bConfigurationValue ) < 0 ) {
1933
                printk(KERN_INFO "usb-midi: Could not set config.\n");
1934
                return -EINVAL;
1935
        }
1936
 
1937
        /* From USB Spec v2.0, Section 9.5.
1938
           If the class or vendor specific descriptors use the same format
1939
           as standard descriptors (e.g., start with a length byte and
1940
           followed by a type byte), they must be returned interleaved with
1941
           standard descriptors in the configuration information returned by
1942
           a GetDescriptor(Configuration) request. In this case, the class
1943
           or vendor-specific descriptors must follow a related standard
1944
           descriptor they modify or extend.
1945
        */
1946
 
1947
        ret = usb_get_descriptor( d, USB_DT_CONFIG, i, buf, USB_DT_CONFIG_SIZE );
1948
        if ( ret < 0 ) {
1949
                printk(KERN_INFO "usb-midi: Could not get config (error=%d).\n", ret);
1950
                return -EINVAL;
1951
        }
1952
        if ( buf[1] != USB_DT_CONFIG || buf[0] < USB_DT_CONFIG_SIZE ) {
1953
                printk(KERN_INFO "usb-midi: config not as expected.\n");
1954
                return -EINVAL;
1955
        }
1956
        bufSize = buf[2] | buf[3]<<8;
1957
        buffer = (unsigned char *)kmalloc(sizeof(unsigned char)*bufSize, GFP_KERNEL);
1958
        if ( !buffer ) {
1959
                printk(KERN_INFO "usb-midi: Could not allocate memory.\n");
1960
                return -EINVAL;
1961
        }
1962
        ret = usb_get_descriptor( d, USB_DT_CONFIG, i, buffer, bufSize );
1963
        if ( ret < 0 ) {
1964
                printk(KERN_INFO "usb-midi: Could not get full config (error=%d).\n", ret);
1965
                kfree(buffer);
1966
                return -EINVAL;
1967
        }
1968
 
1969
        u = parse_descriptor( d, buffer, bufSize, ifnum, alts, 0);
1970
        kfree(buffer);
1971
        if ( u == NULL ) {
1972
                return -EINVAL;
1973
        }
1974
 
1975
        ret = alloc_usb_midi_device( d, s, u );
1976
 
1977
        kfree(u);
1978
 
1979
        return ret;
1980
}
1981
 
1982
 
1983
/** When user has requested a specific device, match it exactly.
1984
 *
1985
 * Uses uvendor, uproduct, uinterface, ualt, umin, umout and ucable.
1986
 * Called by usb_midi_probe();
1987
 *
1988
 **/
1989
static int detect_by_hand(struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s)
1990
{
1991
        struct usb_midi_device u;
1992
 
1993
        if ( d->descriptor.idVendor != uvendor ||
1994
             d->descriptor.idProduct != uproduct ||
1995
             ifnum != uinterface ) {
1996
                return -EINVAL;
1997
        }
1998
 
1999
        if ( ualt < 0 ) { ualt = -1; }
2000
 
2001
        if ( umin   < 0 || umin   > 15 ) { umin   = 0x01 | USB_DIR_IN; }
2002
        if ( umout  < 0 || umout  > 15 ) { umout  = 0x01; }
2003
        if ( ucable < 0 || ucable > 15 ) { ucable = 0; }
2004
 
2005
        u.deviceName = 0; /* A flag for alloc_usb_midi_device to get device name
2006
                             from device. */
2007
        u.idVendor   = uvendor;
2008
        u.idProduct  = uproduct;
2009
        u.interface  = uinterface;
2010
        u.altSetting = ualt;
2011
 
2012
        u.in[0].endpoint    = umin;
2013
        u.in[0].cableId     = (1<<ucable);
2014
 
2015
        u.out[0].endpoint   = umout;
2016
        u.out[0].cableId    = (1<<ucable);
2017
 
2018
        return alloc_usb_midi_device( d, s, &u );
2019
}
2020
 
2021
 
2022
 
2023
/* ------------------------------------------------------------------------- */
2024
 
2025
static void *usb_midi_probe(struct usb_device *dev, unsigned int ifnum,
2026
                            const struct usb_device_id *id)
2027
{
2028
        struct usb_midi_state *s;
2029
 
2030
        s = (struct usb_midi_state *)kmalloc(sizeof(struct usb_midi_state), GFP_KERNEL);
2031
        if ( !s ) { return NULL; }
2032
 
2033
        memset( s, 0, sizeof(struct usb_midi_state) );
2034
        INIT_LIST_HEAD(&s->midiDevList);
2035
        INIT_LIST_HEAD(&s->inEndpointList);
2036
        INIT_LIST_HEAD(&s->outEndpointList);
2037
        s->usbdev = dev;
2038
        s->count  = 0;
2039
        spin_lock_init(&s->lock);
2040
 
2041
        if (
2042
                detect_by_hand( dev, ifnum, s ) &&
2043
                detect_midi_subclass( dev, ifnum, s ) &&
2044
                detect_vendor_specific_device( dev, ifnum, s ) &&
2045
                detect_yamaha_device( dev, ifnum, s) ) {
2046
                kfree(s);
2047
                return NULL;
2048
        }
2049
 
2050
        down(&open_sem);
2051
        list_add_tail(&s->mididev, &mididevs);
2052
        up(&open_sem);
2053
 
2054
#ifdef MOD_INC_EACH_PROBE
2055
        MOD_INC_USE_COUNT;
2056
#endif
2057
 
2058
        return s;
2059
}
2060
 
2061
 
2062
static void usb_midi_disconnect(struct usb_device *dev, void *ptr)
2063
{
2064
        struct usb_midi_state *s = (struct usb_midi_state *)ptr;
2065
        struct list_head      *list;
2066
        struct usb_mididev    *m;
2067
 
2068
        if ( s == (struct usb_midi_state *)-1 ) {
2069
                return;
2070
        }
2071
        if ( !s->usbdev ) {
2072
                return;
2073
        }
2074
        down(&open_sem);
2075
        list_del(&s->mididev);
2076
        INIT_LIST_HEAD(&s->mididev);
2077
        s->usbdev = NULL;
2078
 
2079
        for ( list = s->midiDevList.next; list != &s->midiDevList; list = list->next ) {
2080
                m = list_entry(list, struct usb_mididev, list);
2081
                wake_up(&(m->min.ep->wait));
2082
                wake_up(&(m->mout.ep->wait));
2083
                if ( m->dev_midi >= 0 ) {
2084
                        unregister_sound_midi(m->dev_midi);
2085
                }
2086
                m->dev_midi = -1;
2087
        }
2088
        release_midi_device(s);
2089
        wake_up(&open_wait);
2090
#ifdef MOD_INC_EACH_PROBE
2091
        MOD_DEC_USE_COUNT;
2092
#endif
2093
 
2094
        return;
2095
}
2096
 
2097
 
2098
 
2099
static struct usb_driver usb_midi_driver = {
2100
        name: "midi",
2101
        probe: usb_midi_probe,
2102
        disconnect: usb_midi_disconnect,
2103
        id_table:       NULL,                   /* check all devices */
2104
        driver_list: LIST_HEAD_INIT(usb_midi_driver.driver_list)
2105
};
2106
 
2107
/* ------------------------------------------------------------------------- */
2108
 
2109
int __init usb_midi_init(void)
2110
{
2111
        if ( usb_register(&usb_midi_driver) < 0 )
2112
                return -1;
2113
 
2114
        return 0;
2115
 
2116
}
2117
 
2118
void __exit usb_midi_exit(void)
2119
{
2120
        usb_deregister(&usb_midi_driver);
2121
}
2122
 
2123
module_init(usb_midi_init) ;
2124
module_exit(usb_midi_exit) ;
2125
 
2126
#ifdef HAVE_ALSA_SUPPORT
2127
#define SNDRV_MAIN_OBJECT_FILE
2128
#include "../../include/driver.h"
2129
#include "../../include/control.h"
2130
#include "../../include/info.h"
2131
#include "../../include/cs46xx.h"
2132
 
2133
/* ------------------------------------------------------------------------- */
2134
 
2135
static int snd_usbmidi_input_close(snd_rawmidi_substream_t * substream)
2136
{
2137
        return 0;
2138
}
2139
 
2140
static int snd_usbmidi_input_open(snd_rawmidi_substream_t * substream )
2141
{
2142
        return 0;
2143
}
2144
 
2145
static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t * substream, int up)
2146
{
2147
        return 0;
2148
}
2149
 
2150
 
2151
/* ------------------------------------------------------------------------- */
2152
 
2153
static int snd_usbmidi_output_close(snd_rawmidi_substream_t * substream)
2154
{
2155
        return 0;
2156
}
2157
 
2158
static int snd_usbmidi_output_open(snd_rawmidi_substream_t * substream)
2159
{
2160
        return 0;
2161
}
2162
 
2163
static void snd_usb_midi_output_trigger(snd_rawmidi_substream_t * substream,
2164
                                        int up)
2165
{
2166
        return 0;
2167
}
2168
 
2169
/* ------------------------------------------------------------------------- */
2170
 
2171
static snd_rawmidi_ops_t snd_usbmidi_output =
2172
{
2173
        open:           snd_usbmidi_output_open,
2174
        close:          snd_usbmidi_output_close,
2175
        trigger:        snd_usbmidi_output_trigger,
2176
};
2177
static snd_rawmidi_ops_t snd_usbmidi_input =
2178
{
2179
        open:           snd_usbmidi_input_open,
2180
        close:          snd_usbmidi_input_close,
2181
        trigger:        snd_usbmidi_input_trigger,
2182
};
2183
 
2184
int snd_usbmidi_midi(cs46xx_t *chip, int device, snd_rawmidi_t **rrawmidi)
2185
{
2186
        snd_rawmidi_t *rmidi;
2187
        int err;
2188
 
2189
        if (rrawmidi)
2190
                *rrawmidi = NULL;
2191
        if ((err = snd_rawmidi_new(chip->card, "USB-MIDI", device, 1, 1, &rmidi)) < 0)
2192
                return err;
2193
        strcpy(rmidi->name, "USB-MIDI");
2194
 
2195
        snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output );
2196
        snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input );
2197
 
2198
        rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
2199
 
2200
        rmidi->private_data = chip;
2201
        chip->rmidi = rmidi;
2202
        if (rrawmidi)
2203
                *rrawmidi = NULL;
2204
 
2205
        return 0;
2206
}
2207
 
2208
int snd_usbmidi_create( snd_card_t * card,
2209
                        struct pci_dev * pci,
2210
                        usbmidi_t ** rchip )
2211
{
2212
        usbmidi_t *chip;
2213
        int err, idx;
2214
        snd_region_t *region;
2215
        static snd_device_opt_t ops = {
2216
                dev_free: snd_usbmidi_dev_free,
2217
        };
2218
 
2219
        *rchip = NULL;
2220
        chip = snd_magic_kcalloc( usbmidi_t, 0, GFP_KERNEL );
2221
        if ( chip == NULL )
2222
                return -ENOMEM;
2223
}
2224
 
2225
EXPORT_SYMBOL(snd_usbmidi_create);
2226
EXPORT_SYMBOL(snd_usbmidi_midi);
2227
#endif /* HAVE_ALSA_SUPPORT */
2228
 

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.