?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 32

Line No. Rev Author Line
1 32 kaklik /******************************************************************************
2  
3 USB Host Audio v1.0 Client Driver
4  
5 This is the Audio v1.0 Class Client driver file for a USB Embedded Host device.
6 This file should be used in a project with usb_host.c to provided the USB
7 hardware interface.
8  
9 To interface with usb_host.c, the routine USBHostAudioV1Initialize() should be
10 specified as the Initialize() function, and USBHostAudioV1EventHandler() should
11 be specified as the EventHandler() function in the usbClientDrvTable[] array
12 declared in usb_config.h.
13  
14 This driver utilizes transfer events from the host driver; therefore,
15 USB_ENABLE_TRANSFER_EVENT must be defined.
16  
17 Since audio support is performed with both isochronous and interrupt transfers,
18 USB_SUPPORT_INTERRUPT_TRANSFERS and USB_SUPPORT_ISOCHRONOUS_TRANSFERS
19 must be defined.
20  
21 * FileName: usb_host_audio_v1.c
22 * Dependencies: None
23 * Processor: PIC24/dsPIC30/dsPIC33/PIC32MX
24 * Compiler: C30 v3.12/C32 v1.05
25 * Company: Microchip Technology, Inc.
26  
27 Software License Agreement
28  
29 The software supplied herewith by Microchip Technology Incorporated
30 (the “Company”) for its PICmicro® Microcontroller is intended and
31 supplied to you, the Company’s customer, for use solely and
32 exclusively on Microchip PICmicro Microcontroller products. The
33 software is owned by the Company and/or its supplier, and is
34 protected under applicable copyright laws. All rights are reserved.
35 Any use in violation of the foregoing restrictions may subject the
36 user to criminal sanctions under applicable laws, as well as to
37 civil liability for the breach of the terms and conditions of this
38 license.
39  
40 THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
41 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
42 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
43 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
44 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
45 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
46  
47 Change History:
48 Rev Description
49 ----- -----------
50 2.7 Initial release
51  
52 2.7a Fixed casting issue that could result in incorrect behavior on
53 PIC32 parts.
54  
55 Added USBHostAudioV1DataEventHandler() function.
56  
57 *******************************************************************************/
58  
59  
60 #include <stdlib.h>
61 #include <string.h>
62 #include "GenericTypeDefs.h"
63 #include "HardwareProfile.h"
64 #include "USB\usb.h"
65 #include "USB\usb_host_audio_v1.h"
66  
67 //#define DEBUG_MODE
68 #ifdef DEBUG_MODE
69 #include "uart2.h"
70 #endif
71  
72  
73 // *****************************************************************************
74 // *****************************************************************************
75 // Section: Configuration
76 // *****************************************************************************
77 // *****************************************************************************
78  
79 // *****************************************************************************
80 /* Confirm Configuration
81  
82 Ensure that all of the proper configurations have been set up for this client
83 driver.
84 */
85  
86 #ifndef USB_SUPPORT_INTERRUPT_TRANSFERS
87 #error The Audio v1.0 Client Driver requires interrupt transfers.
88 #endif
89  
90 #ifndef USB_SUPPORT_ISOCHRONOUS_TRANSFERS
91 #error The Audio v1.0 Client Driver requires isochronous transfers.
92 #endif
93  
94 #ifndef USB_ENABLE_TRANSFER_EVENT
95 #error The Audio v1.0 Client Driver requires transfer events.
96 #endif
97  
98  
99 // *****************************************************************************
100 /* Max Number of Supported Devices
101  
102 This value represents the maximum number of attached devices this class driver
103 can support. If the user does not define a value, it will be set to 1.
104 Currently this must be set to 1, due to limitations in the USB Host layer.
105 */
106 #ifndef USB_MAX_AUDIO_DEVICES
107 #define USB_MAX_AUDIO_DEVICES 1
108 #endif
109  
110 // *****************************************************************************
111 // *****************************************************************************
112 // Section: Constants
113 // *****************************************************************************
114 // *****************************************************************************
115  
116 #define USB_CLASS_AUDIO 0x01
117  
118 #define USB_SUBCLASS_AUDIO_UNDEFINED 0x00
119 #define USB_SUBCLASS_AUDIO_CONTROL 0x01
120 #define USB_SUBCLASS_AUDIO_STREAMING 0x02
121 #define USB_SUBCLASS_AUDIO_MIDISTREAMING 0x03
122  
123 #define USB_AUDIO_DESCRIPTOR_UNDEFINED 0x20
124 #define USB_AUDIO_DESCRIPTOR_DEVICE 0x21
125 #define USB_AUDIO_DESCRIPTOR_CONFIGURATION 0x22
126 #define USB_AUDIO_DESCRIPTOR_STRING 0x23
127 #define USB_AUDIO_DESCRIPTOR_INTERFACE 0x24
128 #define USB_AUDIO_DESCRIPTOR_ENDPOINT 0x25
129  
130 #define USB_AUDIO_AC_IF_DESCRIPTOR_UNDEFINED 0x00
131 #define USB_AUDIO_AC_IF_DESCRIPTOR_HEADER 0x01
132 #define USB_AUDIO_AC_IF_DESCRIPTOR_INPUT_TERMINAL 0x02
133 #define USB_AUDIO_AC_IF_DESCRIPTOR_OUTPUT_TERMINAL 0x03
134 #define USB_AUDIO_AC_IF_DESCRIPTOR_MIXER_UNIT 0x04
135 #define USB_AUDIO_AC_IF_DESCRIPTOR_SELECTOR_UNIT 0x05
136 #define USB_AUDIO_AC_IF_DESCRIPTOR_FEATURE_UNIT 0x06
137 #define USB_AUDIO_AC_IF_DESCRIPTOR_PROCESSING_UNIT 0x07
138 #define USB_AUDIO_AC_IF_DESCRIPTOR_EXTENSION_UNIT 0x08
139  
140 #define USB_AUDIO_AS_IF_DESCRIPTOR_UNDEFINED 0x00
141 #define USB_AUDIO_AS_IF_DESCRIPTOR_GENERAL 0x01
142 #define USB_AUDIO_AS_IF_DESCRIPTOR_FORMAT_TYPE 0x02
143 #define USB_AUDIO_AS_IF_DESCRIPTOR_FORMAT_SPECIFIC 0x03
144  
145 #define USB_AUDIO_FORMAT_TYPE_UNDEFINED 0x00
146 #define USB_AUDIO_FORMAT_TYPE_I 0x01
147 #define USB_AUDIO_FORMAT_TYPE_II 0x02
148 #define USB_AUDIO_FORMAT_TYPE_III 0x03
149  
150 // *****************************************************************************
151 // Section: Device Request Constants
152 // *****************************************************************************
153  
154 // Class-specific Request Codes
155 #define SET_CUR 0x01
156 #define GET_CUR 0x81
157 #define SET_MIN 0x02
158 #define GET_MIN 0x82
159 #define SET_MAX 0x03
160 #define GET_MAX 0x83
161 #define SET_RES 0x04
162 #define GET_RES 0x84
163 #define SET_MEM 0x05
164 #define GET_MEM 0x85
165 #define GET_STAT 0xFF
166  
167 // Endpoint Control Selectors
168 #if defined( CONTROL_SELECTOR_IN_LOW_BYTE )
169 // Some devices require the CS value in the low byte, which contradicts the USB Audio v1.0 spec
170 #define SAMPLING_FREQ_CONTROL 0x01
171 #define PITCH_CONTROL 0x02
172 #else
173 #define SAMPLING_FREQ_CONTROL 0x0100
174 #define PITCH_CONTROL 0x0200
175 #endif
176  
177 // *****************************************************************************
178 // Section: Other Constants
179 // *****************************************************************************
180  
181  
182  
183 //******************************************************************************
184 //******************************************************************************
185 // Section: Data Structures
186 //******************************************************************************
187 //******************************************************************************
188  
189 // *****************************************************************************
190 /* USB Audio Device Information
191  
192 This structure is used to hold all the information about an attached Mass Storage device.
193 */
194 typedef struct _USB_AUDIO_DEVICE_INFO
195 {
196 USB_AUDIO_V1_DEVICE_ID ID;
197 BYTE *pFormatTypeDescriptor;
198 union
199 {
200 struct
201 {
202 BYTE bfFoundZeroBandwidthSetting : 1;
203 BYTE bfFoundFullBandwidthSetting : 1;
204 BYTE bfSettingInterface : 1;
205 BYTE bfSettingFrequency : 1;
206 };
207 BYTE val;
208 } flags;
209 BYTE interface; // Interface number we are using.
210 BYTE settingZeroBandwidth; // The zero bandwidth alternate setting.
211 BYTE settingFullBandwidth; // The full bandwidth alternate setting.
212 BYTE endpointAudioStream; // Streaming audio endpoint.
213 } USB_AUDIO_DEVICE_INFO;
214  
215  
216  
217 //******************************************************************************
218 //******************************************************************************
219 // Section: Local Prototypes
220 //******************************************************************************
221 //******************************************************************************
222  
223  
224  
225 //******************************************************************************
226 //******************************************************************************
227 // Section: Macros
228 //******************************************************************************
229 //******************************************************************************
230  
231  
232  
233 //******************************************************************************
234 //******************************************************************************
235 // Section: Audio V1.0 Host Global Variables
236 //******************************************************************************
237 //******************************************************************************
238  
239 static USB_AUDIO_DEVICE_INFO deviceInfoAudioV1[USB_MAX_AUDIO_DEVICES] __attribute__ ((aligned));
240  
241  
242 // *****************************************************************************
243 // *****************************************************************************
244 // Section: Application Callable Functions
245 // *****************************************************************************
246 // *****************************************************************************
247  
248  
249 /****************************************************************************
250 Function:
251 BYTE USBHostAudioV1ReceiveAudioData( BYTE deviceAddress,
252 ISOCHRONOUS_DATA *pIsochronousData )
253  
254 Summary:
255 This function starts the reception of streaming, isochronous audio data.
256  
257 Description:
258 This function starts the reception of streaming, isochronous audio data.
259  
260 Precondition:
261 USBHostAudioV1SetInterfaceFullBandwidth() must be called to set the
262 device to its full bandwidth interface.
263  
264 Parameters:
265 BYTE deviceAddress - Device address
266 ISOCHRONOUS_DATA *pIsochronousData - Pointer to an ISOCHRONOUS_DATA
267 structure, containing information for the
268 application and the host driver for the
269 isochronous transfer.
270  
271 Return Values:
272 USB_SUCCESS - Request started successfully
273 USB_AUDIO_DEVICE_NOT_FOUND - No device with specified address
274 USB_AUDIO_DEVICE_BUSY - Device is already receiving audio data or
275 setting an interface.
276 Others - See USBHostIssueDeviceRequest() errors.
277  
278 Remarks:
279 Some devices require other operations between setting the full bandwidth
280 interface and starting the streaming audio data. Therefore, these two
281 functions are broken out separately.
282 ***************************************************************************/
283  
284 BYTE USBHostAudioV1ReceiveAudioData( BYTE deviceAddress,
285 ISOCHRONOUS_DATA *pIsochronousData )
286 {
287 DWORD byteCount;
288 BYTE errorCode;
289 BYTE i;
290  
291 #ifdef DEBUG_MODE
292 UART2PrintString( "AUD: Start audio stream\r\n" );
293 #endif
294  
295 // Find the correct device.
296 for (i=0; (i<USB_MAX_AUDIO_DEVICES) && (deviceInfoAudioV1[i].ID.deviceAddress != deviceAddress); i++);
297 if (i == USB_MAX_AUDIO_DEVICES)
298 {
299 return USB_AUDIO_DEVICE_NOT_FOUND;
300 }
301  
302 // Make sure the device is not already receiving data or setting the interface.
303 if (!USBHostTransferIsComplete( deviceInfoAudioV1[i].ID.deviceAddress,
304 deviceInfoAudioV1[i].endpointAudioStream, &errorCode, &byteCount ) ||
305 deviceInfoAudioV1[i].flags.bfSettingInterface)
306 {
307 return USB_AUDIO_DEVICE_BUSY;
308 }
309  
310 // Start receiving data
311 errorCode = USBHostReadIsochronous( deviceInfoAudioV1[i].ID.deviceAddress,
312 deviceInfoAudioV1[i].endpointAudioStream, pIsochronousData );
313 if (errorCode)
314 {
315 #ifdef DEBUG_MODE
316 UART2PrintString( "AUD: Cannot start audio stream.\r\n" );
317 #endif
318 }
319  
320 return errorCode;
321 }
322  
323  
324 /****************************************************************************
325 Function:
326 BYTE USBHostAudioV1SetInterfaceFullBandwidth( BYTE deviceAddress )
327  
328 Summary:
329 This function sets the full bandwidth interface.
330  
331 Description:
332 This function sets the full bandwidth interface. This function should
333 be called before calling USBHostAudioV1ReceiveAudioData() to receive the
334 audio stream. Upon completion, the event EVENT_AUDIO_INTERFACE_SET will
335 be generated.
336  
337 Precondition:
338 None
339  
340 Parameters:
341 BYTE deviceAddress - Device address
342  
343 Return Values:
344 USB_SUCCESS - Request started successfully
345 USB_AUDIO_DEVICE_NOT_FOUND - No device with specified address
346 USB_AUDIO_DEVICE_BUSY - Device is already receiving audio data or
347 setting an interface.
348 Others - See USBHostIssueDeviceRequest() errors.
349  
350 Remarks:
351 None
352 ***************************************************************************/
353  
354 BYTE USBHostAudioV1SetInterfaceFullBandwidth( BYTE deviceAddress )
355 {
356 DWORD byteCount;
357 BYTE errorCode;
358 BYTE i;
359  
360 #ifdef DEBUG_MODE
361 UART2PrintString( "AUD: Start audio stream\r\n" );
362 #endif
363  
364 // Find the correct device.
365 for (i=0; (i<USB_MAX_AUDIO_DEVICES) && (deviceInfoAudioV1[i].ID.deviceAddress != deviceAddress); i++);
366 if (i == USB_MAX_AUDIO_DEVICES)
367 {
368 return USB_AUDIO_DEVICE_NOT_FOUND;
369 }
370  
371 // Make sure the device is not already receiving data.
372 if (!USBHostTransferIsComplete( deviceInfoAudioV1[i].ID.deviceAddress,
373 deviceInfoAudioV1[i].endpointAudioStream, &errorCode, &byteCount ) ||
374 deviceInfoAudioV1[i].flags.bfSettingInterface)
375 {
376 return USB_AUDIO_DEVICE_BUSY;
377 }
378  
379 // Set the interface to the full bandwidth setting.
380 errorCode = USBHostIssueDeviceRequest( deviceInfoAudioV1[i].ID.deviceAddress, 0x01, USB_REQUEST_SET_INTERFACE,
381 deviceInfoAudioV1[i].settingFullBandwidth, deviceInfoAudioV1[i].interface, 0, NULL, USB_DEVICE_REQUEST_SET,
382 deviceInfoAudioV1[i].ID.clientDriverID );
383 if (errorCode)
384 {
385 #ifdef DEBUG_MODE
386 UART2PrintString( "AUD: Cannot change to full bandwidth interface.\r\n" );
387 #endif
388 }
389 else
390 {
391 deviceInfoAudioV1[i].flags.bfSettingInterface = 1;
392 }
393  
394 return errorCode;
395 }
396  
397  
398 /****************************************************************************
399 Function:
400 BYTE USBHostAudioV1SetInterfaceZeroBandwidth( BYTE deviceAddress )
401  
402 Summary:
403 This function sets the zero bandwidth interface.
404  
405 Description:
406 This function sets the full bandwidth interface. This function can
407 be called after calling USBHostAudioV1TerminateTransfer() to terminate the
408 audio stream. Upon completion, the event EVENT_AUDIO_INTERFACE_SET will
409 be generated.
410  
411 Precondition:
412 None
413  
414 Parameters:
415 BYTE deviceAddress - Device address
416  
417 Return Values:
418 USB_SUCCESS - Request started successfully
419 USB_AUDIO_DEVICE_NOT_FOUND - No device with the specified address.
420 Others - See USBHostIssueDeviceRequest()
421  
422 Remarks:
423 None
424 ***************************************************************************/
425  
426 BYTE USBHostAudioV1SetInterfaceZeroBandwidth( BYTE deviceAddress )
427 {
428 DWORD byteCount;
429 BYTE errorCode;
430 BYTE i;
431  
432 // Find the correct device.
433 for (i=0; (i<USB_MAX_AUDIO_DEVICES) && (deviceInfoAudioV1[i].ID.deviceAddress != deviceAddress); i++);
434 if (i == USB_MAX_AUDIO_DEVICES)
435 {
436 return USB_AUDIO_DEVICE_NOT_FOUND;
437 }
438  
439 // Make sure the device is not already receiving data.
440 if (!USBHostTransferIsComplete( deviceInfoAudioV1[i].ID.deviceAddress,
441 deviceInfoAudioV1[i].endpointAudioStream, &errorCode, &byteCount ) ||
442 deviceInfoAudioV1[i].flags.bfSettingInterface)
443 {
444 return USB_AUDIO_DEVICE_BUSY;
445 }
446  
447 // Set the interface to the zero bandwidth setting.
448 errorCode = USBHostIssueDeviceRequest( deviceInfoAudioV1[i].ID.deviceAddress, 0x01, USB_REQUEST_SET_INTERFACE,
449 deviceInfoAudioV1[i].settingZeroBandwidth, deviceInfoAudioV1[i].interface, 0, NULL, USB_DEVICE_REQUEST_SET,
450 deviceInfoAudioV1[i].ID.clientDriverID );
451  
452 if (errorCode)
453 {
454 #ifdef DEBUG_MODE
455 UART2PrintString( "AUD: Cannot change to zero bandwidth interface.\r\n" );
456 #endif
457 }
458 else
459 {
460 deviceInfoAudioV1[i].flags.bfSettingInterface = 1;
461 }
462  
463 return errorCode;
464 }
465  
466 /****************************************************************************
467 Function:
468 BYTE USBHostAudioV1SetSamplingFrequency( BYTE deviceAddress, BYTE *frequency )
469  
470 Summary:
471 This function sets the sampling frequency for the device.
472  
473 Description:
474 This function sets the sampling frequency for the device. If the exact
475 frequency is not supported by the device, the device will round it to the
476 closest supported value.
477  
478 IMPORTANT: If the request is initiated successfully, the frequency value
479 must remain valid until the EVENT_AUDIO_FREQUENCY_SET event is received.
480 Therefore, this value cannot be a local (stack) variable. The application
481 can either use a global variable for this value, or it can use the
482 function USBHostAudioV1SupportedFrequencies() to obtain a pointer to the
483 number and list of supported frequencies, and pass a pointer to the desired
484 frequency in this list.
485  
486 Precondition:
487 None
488  
489 Parameters:
490 BYTE deviceAddress - Device address
491 BYTE *frequency - Pointer to three bytes that specify the desired
492 sampling frequency. NOTE: If the request is
493 initiated successfully, this location must
494 remain valid until the EVENT_AUDIO_FREQUENCY_SET
495 event is received.
496  
497 Return Values:
498 USB_SUCCESS - Request started successfully
499 Others - See USBHostIssueDeviceRequest() errors.
500  
501 Example:
502 <code>
503 BYTE numFrequencies;
504 BYTE *ptr;
505  
506 ptr = USBHostAudioV1SupportedFrequencies( deviceAddress );
507 if (ptr)
508 {
509 numFrequencies = *ptr;
510 ptr++;
511 if (numFrequencies == 0)
512 {
513 // Continuous sampling, minimum and maximum are specified.
514 DWORD minFrequency;
515 DWORD maxFrequency;
516  
517 minFrequency = *ptr + (*(ptr+1) << 8) + (*(ptr+2) << 16);
518 ptr += 3;
519 maxFrequency = *ptr + (*(ptr+1) << 8) + (*(ptr+2) << 16);
520 if ((minFrequency <= desiredFrequency) && (desiredFrequency <= maxFrequency))
521 {
522 rc = USBHostAudioV1SetSamplingFrequency( deviceAddress, &desiredFrequency );
523 }
524 else
525 {
526 // Desired frequency out of range
527 }
528 }
529 else
530 {
531 // Discrete sampling frequencies are specified.
532 DWORD frequency;
533  
534 while (numFrequencies)
535 {
536 frequency = *ptr + (*(ptr+1) << 8) + (*(ptr+2) << 16);
537 if (frequency == desiredFrequency)
538 {
539 rc = USBHostAudioV1SetSamplingFrequency( deviceAddress, ptr );
540 continue;
541 }
542 numFrequencies--;
543 ptr += 3;
544 }
545 if (numFrequencies == 0)
546 {
547 // Desired frequency not found.
548 }
549 }
550 }
551 </code>
552  
553 Remarks:
554 If a global variable is used to old the frequency, it can be declared as
555 a DWORD. Since PIC Microcontrollers are little endian machines, a
556 pointer to the DWORD can be used as the frequency parameter:
557 <code>
558 DWORD desiredFrequency = 44100; // Hertz
559  
560 rc = USBHostAudioV1SetSamplingFrequency( deviceAddress, (BYTE *)(&desiredFrequency) );
561 </code>
562  
563 ***************************************************************************/
564  
565 BYTE USBHostAudioV1SetSamplingFrequency( BYTE deviceAddress, BYTE *frequency )
566 {
567 BYTE errorCode;
568 BYTE i;
569  
570 // Find the correct device.
571 for (i=0; (i<USB_MAX_AUDIO_DEVICES) && (deviceInfoAudioV1[i].ID.deviceAddress != deviceAddress); i++);
572 if (i == USB_MAX_AUDIO_DEVICES)
573 {
574 return USB_AUDIO_DEVICE_NOT_FOUND;
575 }
576  
577 // Issue the device request.
578 errorCode = USBHostIssueDeviceRequest( deviceInfoAudioV1[i].ID.deviceAddress, 0x22, SET_CUR,
579 SAMPLING_FREQ_CONTROL, deviceInfoAudioV1[i].endpointAudioStream, 3, frequency, USB_DEVICE_REQUEST_SET,
580 deviceInfoAudioV1[i].ID.clientDriverID );
581 if (errorCode)
582 {
583 #ifdef DEBUG_MODE
584 UART2PrintString( "AUD: Cannot set sampling frequency.\r\n" );
585 #endif
586 }
587 else
588 {
589 // Set a flag so we will send back the correct event when the request is done.
590 deviceInfoAudioV1[i].flags.bfSettingFrequency = 1;
591 }
592  
593 return errorCode;
594 }
595  
596  
597 /****************************************************************************
598 Function:
599 BYTE * USBHostAudioV1SupportedFrequencies( BYTE deviceAddress )
600  
601 Summary:
602 This function returns a pointer to the list of supported frequencies.
603  
604 Description:
605 This function returns a pointer to the list of supported frequencies. It
606 is intended to be used with the function USBHostAudioV1SetSamplingFrequency()
607 to set the device's sampling frequency.
608  
609 Precondition:
610 None
611  
612 Parameters:
613 BYTE deviceAddress - Device address
614  
615 Returns:
616 This function returns a BYTE pointer to the list of supported frequencies.
617 The first byte of this list is the number of supported frequencies. Each
618 supported frequency is then listed, with three bytes for each frequency.
619  
620 Example:
621 <code>
622 BYTE numFrequencies;
623 BYTE *ptr;
624  
625 ptr = USBHostAudioV1SupportedFrequencies( deviceAddress );
626 if (ptr)
627 {
628 numFrequencies = *ptr;
629 ptr++;
630 if (numFrequencies == 0)
631 {
632 // Continuous sampling, minimum and maximum are specified.
633 DWORD minFrequency;
634 DWORD maxFrequency;
635  
636 minFrequency = *ptr + (*(ptr+1) << 8) + (*(ptr+2) << 16);
637 ptr += 3;
638 maxFrequency = *ptr + (*(ptr+1) << 8) + (*(ptr+2) << 16);
639 if ((minFrequency <= desiredFrequency) && (desiredFrequency <= maxFrequency))
640 {
641 rc = USBHostAudioV1SetSamplingFrequency( deviceAddress, &desiredFrequency );
642 }
643 else
644 {
645 // Desired frequency out of range
646 }
647 }
648 else
649 {
650 // Discrete sampling frequencies are specified.
651 DWORD frequency;
652  
653 while (numFrequencies)
654 {
655 frequency = *ptr + (*(ptr+1) << 8) + (*(ptr+2) << 16);
656 if (frequency == desiredFrequency)
657 {
658 rc = USBHostAudioV1SetSamplingFrequency( deviceAddress, ptr );
659 continue;
660 }
661 numFrequencies--;
662 ptr += 3;
663 }
664 if (numFrequencies == 0)
665 {
666 // Desired frequency not found.
667 }
668 }
669 }
670 </code>
671  
672 Remarks:
673 None
674 ***************************************************************************/
675  
676 BYTE * USBHostAudioV1SupportedFrequencies( BYTE deviceAddress )
677 {
678 BYTE i;
679  
680 // Find the correct device.
681 for (i=0; (i<USB_MAX_AUDIO_DEVICES) && (deviceInfoAudioV1[i].ID.deviceAddress != deviceAddress); i++);
682 if (i == USB_MAX_AUDIO_DEVICES)
683 {
684 return NULL;
685 }
686  
687 #if defined( __PIC32MX__)
688 return (BYTE *)((UINT32)deviceInfoAudioV1[i].pFormatTypeDescriptor + (UINT32)7);
689 #else
690 return (BYTE *)((WORD)deviceInfoAudioV1[i].pFormatTypeDescriptor + (WORD)7);
691 #endif
692 }
693  
694  
695 /****************************************************************************
696 Function:
697 void USBHostAudioV1TerminateTransfer( BYTE deviceAddress )
698  
699 Summary:
700 This function terminates an audio stream.
701  
702 Description:
703 This function terminates an audio stream. It does not change the device's
704 selected interface. The application may wish to call
705 USBHostAudioV1SetInterfaceZeroBandwidth() after this function to set
706 the device to the zero bandwidth interface.
707  
708 Between terminating one audio stream and starting another, the application
709 should call USBHostIsochronousBuffersReset() to reset the data buffers.
710 This is done from the application layer rather than from this function, so
711 the application can process all received audio data.
712  
713 Precondition:
714 None
715  
716 Parameters:
717 BYTE deviceAddress - Device address
718  
719 Returns:
720 None
721  
722 Remarks:
723 None
724 ***************************************************************************/
725  
726 void USBHostAudioV1TerminateTransfer( BYTE deviceAddress )
727 {
728 BYTE i;
729  
730 // Find the correct device.
731 for (i=0; (i<USB_MAX_AUDIO_DEVICES) && (deviceInfoAudioV1[i].ID.deviceAddress != deviceAddress); i++);
732 if (i == USB_MAX_AUDIO_DEVICES)
733 {
734 return;
735 }
736  
737 // Terminate any endpoint tranfers that are occurring.
738 USBHostTerminateTransfer( deviceInfoAudioV1[i].ID.deviceAddress, deviceInfoAudioV1[i].endpointAudioStream );
739  
740 return;
741 }
742  
743 // *****************************************************************************
744 // *****************************************************************************
745 // Section: Host Stack Interface Functions
746 // *****************************************************************************
747 // *****************************************************************************
748  
749 /****************************************************************************
750 Function:
751 BOOL USBHostAudioV1DataEventHandler( BYTE address, USB_EVENT event,
752 void *data, DWORD size )
753  
754 Summary:
755 This function is the data event handler for this client driver.
756  
757 Description:
758 This function is the data event handler for this client driver. It is called
759 by the host layer when isochronous data events occur.
760  
761 Precondition:
762 The device has been initialized.
763  
764 Parameters:
765 BYTE address - Address of the device
766 USB_EVENT event - Event that has occurred
767 void *data - Pointer to data pertinent to the event
768 WORD size - Size of the data
769  
770 Return Values:
771 TRUE - Event was handled
772 FALSE - Event was not handled
773  
774 Remarks:
775 The client driver does not need to process the data. Just pass the
776 translated event up to the application layer.
777 ***************************************************************************/
778  
779 BOOL USBHostAudioV1DataEventHandler( BYTE address, USB_EVENT event, void *data, DWORD size )
780 {
781 if (event == EVENT_DATA_ISOC_READ)
782 {
783 return USB_HOST_APP_DATA_EVENT_HANDLER( address, EVENT_AUDIO_STREAM_RECEIVED, data, size );
784 }
785 else
786 {
787 return FALSE;
788 }
789 }
790  
791  
792 /****************************************************************************
793 Function:
794 BOOL USBHostAudioV1Initialize( BYTE address, DWORD flags, BYTE clientDriverID )
795  
796 Summary:
797 This function is the initialization routine for this client driver.
798  
799 Description:
800 This function is the initialization routine for this client driver. It
801 is called by the host layer when the USB device is being enumerated.
802  
803 Precondition:
804 None
805  
806 Parameters:
807 BYTE address - Address of the new device
808 DWORD flags - Initialization flags
809 BYTE clientDriverID - ID to send when issuing a Device Request via
810 USBHostIssueDeviceRequest() or USBHostSetDeviceConfiguration().
811  
812 Return Values:
813 TRUE - We can support the device.
814 FALSE - We cannot support the device.
815  
816 Remarks:
817 None
818 ***************************************************************************/
819  
820 BOOL USBHostAudioV1Initialize( BYTE address, DWORD flags, BYTE clientDriverID )
821 {
822 BYTE *descriptor;
823 BYTE device;
824 WORD i;
825 WORD saveIndex;
826  
827  
828 // See if the device has already been initialized, and therefore is
829 // already in the audio device table. If we change the configuration, the
830 // host driver will call the Initialize function again.
831 for (device = 0; (device < USB_MAX_AUDIO_DEVICES) && (deviceInfoAudioV1[device].ID.deviceAddress != address); device++);
832 if (device < USB_MAX_AUDIO_DEVICES)
833 {
834 return TRUE;
835 }
836  
837 // Find the free slot in the table. If we cannot find one, kick off the device.
838 for (device = 0; (device < USB_MAX_AUDIO_DEVICES) && (deviceInfoAudioV1[device].ID.deviceAddress != 0); device++);
839 if (device == USB_MAX_AUDIO_DEVICES)
840 {
841 #ifdef DEBUG_MODE
842 UART2PrintString( "AUD: No free slots available for audio.\r\n" );
843 #endif
844 // Kick off the device
845 return FALSE;
846 }
847  
848 #ifdef DEBUG_MODE
849 UART2PrintString( "AUD: USBHostAudioV1Initialize(0x" );
850 UART2PutHex( flags );
851 UART2PrintString( ")\r\n" );
852 #endif
853  
854 // Fill in the VID and PID before we lose the device descriptor pointer.
855 // It's not valid unless deviceAddress is non-zero.
856 descriptor = USBHostGetDeviceDescriptor( address );
857 deviceInfoAudioV1[device].ID.vid = ((USB_DEVICE_DESCRIPTOR *)descriptor)->idVendor;
858 deviceInfoAudioV1[device].ID.pid = ((USB_DEVICE_DESCRIPTOR *)descriptor)->idProduct;
859  
860 // Get ready to parse the configuration descriptor.
861 descriptor = USBHostGetCurrentConfigurationDescriptor( address );
862  
863 i = 0;
864  
865 #ifdef DEBUG_MODE
866 UART2PrintString("AUD: Checking descriptor " );
867 UART2PutDec( descriptor[i+5] );
868 UART2PrintString(" ...\r\n" );
869 #endif
870  
871 // Find the next interface descriptor.
872 while (i < ((USB_CONFIGURATION_DESCRIPTOR *)descriptor)->wTotalLength)
873 {
874 #ifdef DEBUG_MODE
875 UART2PrintString("AUD: Checking interface...\r\n" );
876 #endif
877 // See if we are pointing to an interface descriptor.
878 if (descriptor[i+1] == USB_DESCRIPTOR_INTERFACE)
879 {
880 // See if the interface is a streaming audio interface. Protocol is not used
881 if ((descriptor[i+5] == USB_CLASS_AUDIO) && (descriptor[i+6] == USB_SUBCLASS_AUDIO_STREAMING))
882 {
883 // Record the interface number.
884 deviceInfoAudioV1[device].interface = descriptor[i+2];
885  
886 if (descriptor[i+4] == 0)
887 {
888 // If the number of endpoints is 0, then this is the zero bandwidth configuration.
889 #ifdef DEBUG_MODE
890 UART2PrintString( "AUD: Zero bandwidth configuration\r\n" );
891 #endif
892 deviceInfoAudioV1[device].settingZeroBandwidth = descriptor[i+3];
893 deviceInfoAudioV1[device].flags.bfFoundZeroBandwidthSetting = 1;
894 }
895 else
896 {
897 // If the number of endpoints is not 0, then this is the full bandwidth configuration.
898 #ifdef DEBUG_MODE
899 UART2PrintString( "AUD: Full bandwidth configuration\r\n" );
900 #endif
901 deviceInfoAudioV1[device].settingFullBandwidth = descriptor[i+3];
902 deviceInfoAudioV1[device].flags.bfFoundFullBandwidthSetting = 1;
903  
904 // Initialize variables prior to scanning.
905 i += descriptor[i];
906 saveIndex = i;
907  
908 // Look for an audio stream endpoint.
909 while ((deviceInfoAudioV1[device].endpointAudioStream == 0) &&
910 (i < ((USB_CONFIGURATION_DESCRIPTOR *)descriptor)->wTotalLength))
911 {
912 if (descriptor[i+1] == USB_DESCRIPTOR_ENDPOINT)
913 {
914 if ((descriptor[i+3] & 0x03) == 0x01) // Isochronous
915 {
916 deviceInfoAudioV1[device].endpointAudioStream = descriptor[i+2];
917 deviceInfoAudioV1[device].ID.audioDataPacketSize = (descriptor[i+5] << 8) + descriptor[i+4];
918 #ifdef DEBUG_MODE
919 UART2PrintString( "AUD: Audio stream endpoint: " );
920 UART2PutHex( deviceInfoAudioV1[device].endpointAudioStream );
921 UART2PrintString( ", packet size " );
922 UART2PutHexWord( deviceInfoAudioV1[device].ID.audioDataPacketSize );
923 UART2PrintString( "\r\n" );
924 #endif
925 }
926 }
927 i += descriptor[i];
928 }
929  
930 // Look for the CS_INTERFACE, FORMAT_TYPE descriptor
931 i = saveIndex;
932 while ((deviceInfoAudioV1[device].pFormatTypeDescriptor == NULL) &&
933 (i < ((USB_CONFIGURATION_DESCRIPTOR *)descriptor)->wTotalLength))
934 {
935 if ((descriptor[i+1] == USB_AUDIO_DESCRIPTOR_INTERFACE) &&
936 (descriptor[i+2] == USB_AUDIO_AS_IF_DESCRIPTOR_FORMAT_TYPE))
937 {
938 if (descriptor[i+3] == USB_AUDIO_FORMAT_TYPE_I) // iPods should be only this format.
939 {
940 #ifdef DEBUG_MODE
941 UART2PrintString( "AUD: Found supported frequencies\r\n" );
942 #endif
943 deviceInfoAudioV1[device].pFormatTypeDescriptor = &(descriptor[i]);
944 }
945 }
946 i += descriptor[i];
947 }
948  
949 // Other audio devices may need to check more parameters.
950 // Since we support only iPods, we do not have to check
951 // data format, etc.
952  
953 }
954  
955 // Make sure we've found everything we need.
956 if ((deviceInfoAudioV1[device].flags.bfFoundZeroBandwidthSetting == 1) &&
957 (deviceInfoAudioV1[device].flags.bfFoundFullBandwidthSetting == 1) &&
958 (deviceInfoAudioV1[device].pFormatTypeDescriptor != NULL) &&
959 (deviceInfoAudioV1[device].endpointAudioStream != 0))
960 {
961 // Initialize the device information.
962 deviceInfoAudioV1[device].ID.deviceAddress = address;
963 deviceInfoAudioV1[device].ID.clientDriverID = clientDriverID;
964  
965 #ifdef DEBUG_MODE
966 UART2PrintString( "AUD: AUD attached.\r\n" );
967 #endif
968  
969 // Tell the application layer that we have a device.
970 USB_HOST_APP_EVENT_HANDLER( deviceInfoAudioV1[device].ID.deviceAddress, EVENT_AUDIO_ATTACH, &(deviceInfoAudioV1[device].ID), sizeof(USB_AUDIO_V1_DEVICE_ID) );
971  
972 return TRUE;
973 }
974 }
975 }
976  
977 // Jump to the next descriptor in this configuration.
978 i += descriptor[i];
979 }
980  
981 // This configuration is not valid for an audio device.
982 return FALSE;
983 }
984  
985  
986 /****************************************************************************
987 Function:
988 BOOL USBHostAudioV1EventHandler( BYTE address, USB_EVENT event,
989 void *data, DWORD size )
990  
991 Summary:
992 This function is the event handler for this client driver.
993  
994 Description:
995 This function is the event handler for this client driver. It is called
996 by the host layer when various events occur.
997  
998 Precondition:
999 The device has been initialized.
1000  
1001 Parameters:
1002 BYTE address - Address of the device
1003 USB_EVENT event - Event that has occurred
1004 void *data - Pointer to data pertinent to the event
1005 WORD size - Size of the data
1006  
1007 Return Values:
1008 TRUE - Event was handled
1009 FALSE - Event was not handled
1010  
1011 Remarks:
1012 None
1013 ***************************************************************************/
1014  
1015 BOOL USBHostAudioV1EventHandler( BYTE address, USB_EVENT event, void *data, DWORD size )
1016 {
1017 BYTE i;
1018  
1019 switch (event)
1020 {
1021 case EVENT_NONE: // No event occured (NULL event)
1022 return TRUE;
1023 break;
1024  
1025 case EVENT_DETACH: // USB cable has been detached (data: BYTE, address of device)
1026 #ifdef DEBUG_MODE
1027 UART2PrintString( "AUD: Detach\r\n" );
1028 #endif
1029  
1030 // Find the device in the table. If found, clear the important fields.
1031 for (i=0; (i<USB_MAX_AUDIO_DEVICES) && (deviceInfoAudioV1[i].ID.deviceAddress != address); i++);
1032 if (i < USB_MAX_AUDIO_DEVICES)
1033 {
1034 // Notify that application that the device has been detached.
1035 USB_HOST_APP_EVENT_HANDLER( address, EVENT_AUDIO_DETACH,
1036 &deviceInfoAudioV1[i].ID.deviceAddress, sizeof(BYTE) );
1037  
1038 deviceInfoAudioV1[i].ID.deviceAddress = 0;
1039 deviceInfoAudioV1[i].flags.val = 0;
1040 deviceInfoAudioV1[i].endpointAudioStream = 0;
1041 deviceInfoAudioV1[i].pFormatTypeDescriptor = NULL;
1042 }
1043 return TRUE;
1044 break;
1045  
1046 case EVENT_TRANSFER: // A USB transfer has completed
1047 #ifdef DEBUG_MODE
1048 // UART2PrintString( "AUD: transfer event: " );
1049 // UART2PutHex( address );
1050 // UART2PrintString( "\r\n" );
1051 #endif
1052  
1053 for (i=0; (i<USB_MAX_AUDIO_DEVICES) && (deviceInfoAudioV1[i].ID.deviceAddress != address); i++) {}
1054 if (i == USB_MAX_AUDIO_DEVICES)
1055 {
1056 #ifdef DEBUG_MODE
1057 UART2PrintString( "AUD: Unknown device\r\n" );
1058 #endif
1059 return FALSE;
1060 }
1061  
1062 // The transfer event comes with data of the type HOST_TRANSFER_DATA.
1063  
1064 if (((HOST_TRANSFER_DATA *)data)->bEndpointAddress == 0)
1065 {
1066 if (deviceInfoAudioV1[i].flags.bfSettingInterface)
1067 {
1068 // Either the full or the zero bandwidth interface has been set.
1069 deviceInfoAudioV1[i].flags.bfSettingInterface = 0;
1070 USB_HOST_APP_EVENT_HANDLER( i, EVENT_AUDIO_INTERFACE_SET, NULL, ((HOST_TRANSFER_DATA *)data)->bErrorCode );
1071 }
1072 if (deviceInfoAudioV1[i].flags.bfSettingFrequency)
1073 {
1074 // The sampling frequency has been set.
1075 deviceInfoAudioV1[i].flags.bfSettingFrequency = 0;
1076 USB_HOST_APP_EVENT_HANDLER( i, EVENT_AUDIO_FREQUENCY_SET, data, size );
1077 }
1078 }
1079 #ifdef USB_ENABLE_ISOC_TRANSFER_EVENT
1080 else if (((HOST_TRANSFER_DATA *)data)->bEndpointAddress == deviceInfoAudioV1[i].endpointAudioStream)
1081 {
1082 // If we received streaming audio data, pass the event up to the application.
1083 // It's only one more byte of information more than they need (bmAttributes).
1084 USB_HOST_APP_EVENT_HANDLER( i, EVENT_AUDIO_RECEIVE_STREAM, data, size );
1085 }
1086 #endif
1087 break;
1088  
1089 case EVENT_BUS_ERROR:
1090 #ifdef DEBUG_MODE
1091 // UART2PrintString( "AUD: bus error: " );
1092 // UART2PutHex( address );
1093 // UART2PrintString( "\r\n" );
1094 #endif
1095  
1096 for (i=0; (i<USB_MAX_AUDIO_DEVICES) && (deviceInfoAudioV1[i].ID.deviceAddress != address); i++) {}
1097 if (i == USB_MAX_AUDIO_DEVICES)
1098 {
1099 #ifdef DEBUG_MODE
1100 UART2PrintString( "AUD: Unknown device\r\n" );
1101 #endif
1102 return FALSE;
1103 }
1104  
1105 // The bus error event comes with data of the type HOST_TRANSFER_DATA.
1106  
1107 if (((HOST_TRANSFER_DATA *)data)->bEndpointAddress == 0)
1108 {
1109 ((HOST_TRANSFER_DATA *)data)->bErrorCode = USB_AUDIO_COMMAND_FAILED;
1110 if (deviceInfoAudioV1[i].flags.bfSettingInterface)
1111 {
1112 // The interface could not be set.
1113 deviceInfoAudioV1[i].flags.bfSettingInterface = 0;
1114 USB_HOST_APP_EVENT_HANDLER( i, EVENT_AUDIO_INTERFACE_SET, NULL, ((HOST_TRANSFER_DATA *)data)->bErrorCode );
1115 }
1116 if (deviceInfoAudioV1[i].flags.bfSettingFrequency)
1117 {
1118 // The sampling frequency could not be set.
1119 deviceInfoAudioV1[i].flags.bfSettingFrequency = 0;
1120 USB_HOST_APP_EVENT_HANDLER( i, EVENT_AUDIO_FREQUENCY_SET, data, size );
1121 }
1122 }
1123 else if (((HOST_TRANSFER_DATA *)data)->bEndpointAddress == deviceInfoAudioV1[i].endpointAudioStream)
1124 {
1125 // Bus error on data stream.
1126 USB_HOST_APP_EVENT_HANDLER( i, EVENT_AUDIO_STREAM_RECEIVED, NULL, 0 );
1127 }
1128 break;
1129  
1130 case EVENT_SOF: // Start of frame - NOT NEEDED
1131 case EVENT_RESUME: // Device-mode resume received
1132 case EVENT_SUSPEND: // Device-mode suspend/idle event received
1133 case EVENT_RESET: // Device-mode bus reset received
1134 case EVENT_STALL: // A stall has occured
1135 return TRUE;
1136 break;
1137  
1138 default:
1139 return FALSE;
1140 break;
1141 }
1142 return FALSE;
1143 }
1144  
1145  
1146 // *****************************************************************************
1147 // *****************************************************************************
1148 // Section: Internal Functions
1149 // *****************************************************************************
1150 // *****************************************************************************
1151  
1152  
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3