xref: /inferno-os/emu/MacOSX/deveia.c (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1  /*
2   * Darwin serial port definitions, uses IOKit to build sysdev
3   * Loosely based on FreeBSD/deveia.c
4   * Copyright © 1998, 1999 Lucent Technologies Inc.  All rights reserved.
5   * Revisions Copyright © 1999, 2000 Vita Nuova Limited.  All rights reserved.
6   * Revisions Copyright © 2003 Corpus Callosum Corporation.  All rights reserved.
7  */
8  
9  #include <termios.h>
10  #include <sys/param.h>
11  #include <stdio.h>
12  #include <unistd.h>
13  #include <stdlib.h>
14  
15  #include <mach/mach.h>
16  
17  #include <CoreFoundation/CFNumber.h>
18  
19  #include <IOKit/IOKitLib.h>
20  #include <IOKit/IOCFPlugIn.h>
21  #include <IOKit/usb/IOUSBLib.h>
22  #include <IOKit/serial/IOSerialKeys.h>
23  #include <IOKit/IOBSD.h>
24  
25  #define B14400	14400
26  #define B28800	28800
27  #define B57600	57600
28  #define B76800	76800
29  #define B115200	115200
30  #define B230400	230400
31  
32  extern int vflag;
33  
34  #define MAXDEV 8
35  static char *sysdev[MAXDEV];
36  
37  #include <sys/ioctl.h>
38  #include <sys/ttycom.h>
39  
40  static void _buildsysdev(void);
41  #define	buildsysdev()	_buildsysdev()	/* for devfs-posix.c */
42  
43  static void
44  _buildsysdev(void)
45  {
46      kern_return_t           kernResult;
47      mach_port_t             masterPort;
48      CFMutableDictionaryRef  classesToMatch;
49      io_iterator_t           serialPortIterator;
50      io_object_t             serialDevice;
51      CFMutableArrayRef       array;
52      CFIndex                 idx;
53  
54      kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
55      if (KERN_SUCCESS != kernResult)
56      {
57          printf("IOMasterPort returned %d\n", kernResult);
58      } else {
59          classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
60          if (classesToMatch == NULL)
61          {
62              printf("IOServiceMatching returned a NULL dictionary.\n");
63          } else {
64              CFDictionarySetValue(classesToMatch,
65                                   CFSTR(kIOSerialBSDTypeKey),
66                                   CFSTR(kIOSerialBSDAllTypes));
67          }
68  
69          kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, &serialPortIterator);
70          if (KERN_SUCCESS != kernResult)
71          {
72              printf("IOServiceGetMatchingServices returned %d\n", kernResult);
73          } else {
74              array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
75  
76              while ((serialDevice = IOIteratorNext(serialPortIterator))) {
77                  CFTypeRef	bsdPathAsCFString;
78                  bsdPathAsCFString = IORegistryEntryCreateCFProperty(serialDevice,
79                                                                      CFSTR(kIOCalloutDeviceKey),
80                                                                      kCFAllocatorDefault,
81                                                                      0);
82                  if (bsdPathAsCFString) {
83                      CFArrayAppendValue(array, bsdPathAsCFString);
84                  }
85  
86                  (void) IOObjectRelease(serialDevice);
87              }
88  
89              idx = CFArrayGetCount(array);
90              if (idx > 0) {
91                  Boolean result;
92                  char 	bsdPath[MAXPATHLEN];
93                  char 	*tmpsysdev[idx+1];
94                  CFIndex i;
95  
96                  for (i=0; i<idx; i++) {
97                      result = CFStringGetCString(CFArrayGetValueAtIndex(array, i),
98                                                  bsdPath,
99                                                  sizeof(bsdPath),
100                                                  kCFStringEncodingASCII);
101                      if (result) {
102                          int len = strlen(bsdPath);
103                          tmpsysdev[i] = (char *)malloc((len+1)*sizeof(char));
104                          strcpy(tmpsysdev[i], bsdPath);
105                      }
106                  }
107                  tmpsysdev[idx] = NULL;
108                  for (i=0; i < idx; i++) {
109                      sysdev[i] = tmpsysdev[i];
110                      if (vflag)
111                          printf("BSD path: '%s'\n", sysdev[i]);
112                  }
113              }
114  
115              CFRelease(array);
116          }
117      }
118  
119      if (serialPortIterator)
120          IOObjectRelease(serialPortIterator);
121      if (masterPort)
122          mach_port_deallocate(mach_task_self(), masterPort);
123  
124      return;
125  }
126  
127  #undef nil
128  
129  #include "deveia-posix.c"
130  #include "deveia-bsd.c"
131  
132  static struct tcdef_t bps[] = {
133      {0,             B0},
134      {50,            B50},
135      {75,            B75},
136      {110,           B110},
137      {134,           B134},
138      {150,           B150},
139      {200,           B200},
140      {300,           B300},
141      {600,           B600},
142      {1200,	B1200},
143      {1800,	B1800},
144      {2400,	B2400},
145      {4800,	B4800},
146      {9600,	B9600},
147      {19200,	B19200},
148      {38400,	B38400},
149      {57600,	B57600},
150      {76800,	B76800},
151      {115200,	B115200},
152      {230400,	B230400},
153      {0,		-1}
154  };
155