1
2    /* Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3     *
4     * @APPLE_LICENSE_HEADER_START@
5     *
6     * The contents of this file constitute Original Code as defined in and
7     * are subject to the Apple Public Source License Version 1.1 (the
8     * "License").  You may not use this file except in compliance with the
9     * License.  Please obtain a copy of the License at
10     * http://www.apple.com/publicsource and read it before using this file.
11     *
12     * This Original Code and all software distributed under the License are
13     * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14     * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15     * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16     * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17     * License for the specific language governing rights and limitations
18     * under the License.
19     *
20     * @APPLE_LICENSE_HEADER_END@
21     */
22
23    /*****  For fans of kprintf, IOLog and debugging infrastructure of the  *****/
24    /*****  string ilk, please modify the ELG and PAUSE macros or their *****/
25    /*****  associated EvLog and Pause functions to suit your taste. These  *****/
26    /*****  macros currently are set up to log events to a wraparound   *****/
27    /*****  buffer with minimal performance impact. They take 2 UInt32  *****/
28    /*****  parameters so that when the buffer is dumped 16 bytes per line, *****/
29    /*****  time stamps (~1 microsecond) run down the left side while   *****/
30    /*****  unique 4-byte ASCII codes can be read down the right side.  *****/
31    /*****  Preserving this convention facilitates different maintainers    *****/
32    /*****  using different debugging styles with minimal code clutter. *****/
33
34#ifndef _APPLEIRDA_
35#define _APPLEIRDA_
36
37#include <IOKit/serial/IORS232SerialStreamSync.h>
38#include "IrDAStats.h"
39
40#define DEBUG       0               // for debugging
41#define USE_ELG     0               // to event log - DEBUG must also be set (see below)
42#define kEvLogSize  (4096*16)               // 16 pages = 64K = 4096 events
43#define LOG_DATA    1               // logs data to the IOLog - DEBUG must also be set
44
45#define Sleep_Time  20
46
47#if DEBUG
48    #define IOLogIt(A,B,ASCI,STRING)    IOLog( "AppleIrDA: %p %p " STRING "\n", (void *)(A), (void *)(B) )
49    #if USE_ELG
50	#define ELG(A,B,ASCI,STRING)    EvLog( (void *)(A), (void *)(B), (void *)(ASCI), STRING )
51    #else /* not USE_ELG */
52	#define ELG(A,B,ASCI,STRING)    {IOLog( "AppleIrDA: %p %p " STRING "\n", (void *)(A), (void *)(B) );IOSleep(Sleep_Time);}
53    #endif /* USE_ELG */
54    #if LOG_DATA
55	#define LogData(D, C, b)    DEVLogData((UInt8)D, (UInt32)C, (char *)b)
56    #else /* not LOG_DATA */
57	#define LogData(D, C, b)
58    #endif /* LOG_DATA */
59#else /* not DEBUG */
60    #define IOLogIt(A,B,ASCI,STRING)
61    #define ELG(A,B,ASCI,S)
62    #define LogData(D, C, b)
63    #undef USE_ELG
64    #undef LOG_DATA
65#endif /* DEBUG */
66
67
68#define initIrDAState       true            // False = off, true = on
69
70#define baseName        "IrDA-IrCOMM"
71
72    /* Globals  */
73
74typedef struct IrDAglobals      /* Globals for USB module (not per instance) */
75{
76    UInt32       evLogFlag; // debugging only
77    UInt8       *evLogBuf;
78    UInt8       *evLogBufe;
79    UInt8       *evLogBufp;
80    UInt8       intLevel;
81    class AppleIrDA *AppleIrDAInstance;
82} IrDAglobals;
83
84
85enum ParityType
86{
87    NoParity        = 0,
88    OddParity,
89    EvenParity
90};
91
92#define MAX_BLOCK_SIZE      PAGE_SIZE
93
94#define kDefaultBaudRate    9600
95#define kMaxBaudRate        4000000     // 4Mbs for IrDA
96#define kMinBaudRate        300
97#define kMaxCirBufferSize   4096
98
99    /* IrDA stuff */
100
101struct USBIrDAQoS               // encoded qos values as read from the irda pod (except for driver swabs of shorts)
102{
103    UInt8   bFunctionLength;        // descriptor header (length = 12)
104    UInt8   bDescriptorType;        // descriptor type (0x21)
105    UInt16  version;            // two bytes of version number, should be 0x01, 0x00 for 1.0
106    UInt8   datasize;           // bytes per frame supported
107    UInt8   windowsize;         // 1 thru 7 frames per ack
108    UInt8   minturn;            // min turnaround time
109    UInt8   baud1;              // 16 bits of baud
110    UInt8   baud2;
111    UInt8   bofs;               // number of bofs the pod wants
112    UInt8   sniff;              // 1 if can receive at any speed
113    UInt8   unicast;            // max number of entries in the unicast list
114};
115typedef struct USBIrDAQoS USBIrDAQoS;       // Todo - rename since we use this for SCC as well
116
117    /* SccQueuePrimatives.h */
118
119typedef struct CirQueue
120{
121    UInt8   *Start;
122    UInt8   *End;
123    UInt8   *NextChar;
124    UInt8   *LastChar;
125    size_t  Size;
126    size_t  InQueue;
127} CirQueue;
128
129typedef enum QueueStatus
130{
131    queueNoError = 0,
132    queueFull,
133    queueEmpty,
134    queueMaxStatus
135} QueueStatus;
136
137    /* Inline time conversions */
138
139static inline unsigned long tval2long( mach_timespec val )
140{
141   return (val.tv_sec * NSEC_PER_SEC) + val.tv_nsec;
142}
143
144static inline mach_timespec long2tval( unsigned long val )
145{
146    mach_timespec   tval;
147
148    tval.tv_sec  = val / NSEC_PER_SEC;
149    tval.tv_nsec = val % NSEC_PER_SEC;
150    return tval;
151}
152
153class IrDAComm;
154class AppleIrDASerial;
155
156class AppleIrDA : public IOService      // nub with nice name for user-client connections
157{
158    OSDeclareDefaultStructors(AppleIrDA)
159
160public:
161    static AppleIrDA *withNub(AppleIrDASerial *nub);        // constructor
162    IOReturn    newUserClient( task_t,void*,UInt32, IOUserClient** );
163
164private:
165    AppleIrDASerial *fNub;
166};
167
168
169
170class AppleIrDASerial : public IORS232SerialStreamSync
171{
172    //OSDeclareAbstractStructors(AppleIrDASerial);
173    OSDeclareDefaultStructors(AppleIrDASerial)
174public:
175    virtual void        Add_RXBytes( UInt8 *Buffer, size_t Size ) = 0;
176    virtual SInt16      SetBofCount( SInt16 bof_count ) = 0;
177    virtual UInt16      SetSpeed( UInt32 brate ) = 0;
178    virtual bool        SetUpTransmit( void ) = 0;
179
180    virtual IOReturn    StartTransmit( UInt32 control_length, UInt8 *control_buffer, UInt32 data_length, UInt8 *data_buffer ) = 0;
181    virtual USBIrDAQoS* GetIrDAQoS( void ) = 0;
182    virtual IrDAComm*   GetIrDAComm( void ) = 0;
183    virtual void        GetIrDAStatus( IrDAStatus *status ) = 0;
184    virtual IOReturn    SetIrDAUserClientState( bool IrDAOn ) = 0;
185};
186
187
188#endif
189