1 /* 2 * Copyright 2009-2010, François Revol, <revol@free.fr>. 3 * Sponsored by TuneTracker Systems. 4 * Based on the Haiku usb_serial driver which is: 5 * 6 * Copyright (c) 2007-2008 by Michael Lotz 7 * Heavily based on the original usb_serial driver which is: 8 * 9 * Copyright (c) 2003 by Siarzhuk Zharski <imker@gmx.li> 10 * Distributed under the terms of the MIT License. 11 */ 12 #ifndef _PC_SERIAL_DRIVER_H_ 13 #define _PC_SERIAL_DRIVER_H_ 14 15 #include <OS.h> 16 #include <KernelExport.h> 17 #include <Drivers.h> 18 #include <ISA.h> 19 #include <PCI.h> 20 #include <config_manager.h> 21 #include <string.h> 22 23 #ifdef __HAIKU__ 24 #include <lock.h> 25 #include <new> 26 #else 27 #include "BeOSCompatibility.h" 28 #include "kernel_cpp.h" 29 #endif 30 #include "Tracing.h" 31 32 extern "C" { 33 #ifdef __HAIKU__ 34 #include <tty_module.h> 35 #else 36 #include <ttylayer.h> 37 #endif 38 } 39 40 41 // whether we should handle default COM ports 42 // not for BeOS, it has a "zz" driver for this. 43 #ifdef __HAIKU__ 44 #define HANDLE_ISA_COM 45 #endif 46 47 #define DRIVER_NAME "pc_serial" // driver name for debug output 48 #define DEVICES_COUNT 20 // max simultaneously open devices 49 50 // avoid clashing with BeOS zz driver 51 #define DEVFS_BASE "ports/pc_serial" 52 //#define DEVFS_BASE "ports/serial" 53 54 55 // no user serviceable part beyond this point 56 57 // more PCI serial APIs 58 #ifndef PCI_serial_16650 59 #define PCI_serial_16650 0x03 /* 16650-compatible serial controller */ 60 #define PCI_serial_16750 0x04 /* 16750-compatible serial controller */ 61 #define PCI_serial_16850 0x05 /* 16850-compatible serial controller */ 62 #define PCI_serial_16950 0x06 /* 16950-compatible serial controller */ 63 #endif 64 65 class SerialDevice; 66 67 struct port_constraints { 68 uint32 minsize; 69 uint32 maxsize; 70 uint32 split; // range to split I/O ports for each device 71 uint8 ignoremask; // bitmask of BARs to ignore when probing 72 uint8 maxports; // max number of ports on the card if > 0 73 uint32 subsystem_id_mask; // if set mask with subsys id and shift to get maxports 74 }; 75 76 #define PCI_INVAL 0xffff 77 78 struct serial_support_descriptor { 79 bus_type bus; // B_*_BUS 80 const char *name; 81 const uint32 *bauds; 82 // not yet used 83 SerialDevice *(*instanciator)(struct serial_support_descriptor *desc); 84 // I/O port constrains (which ranges to use, how to split them) 85 struct port_constraints constraints; 86 // bus specific stuff here... 87 struct { 88 // for both ISA & PCI 89 uchar class_base; 90 uchar class_sub; 91 uchar class_api; // or PCI_undefined 92 // for PCI: if PCI_INVAL then match class 93 ushort vendor_id; 94 ushort device_id; 95 ushort subsystem_vendor_id; 96 ushort subsystem_device_id; 97 } match; 98 }; 99 typedef struct serial_support_descriptor serial_support_descriptor; 100 101 102 struct serial_config_descriptor { 103 bus_type bus; // B_*_BUS 104 struct serial_support_descriptor *descriptor; 105 union { 106 struct pci_info pci; 107 } d; 108 }; 109 110 111 /* Some usefull helper defines ... */ 112 #define SIZEOF(array) (sizeof(array) / sizeof(array[0])) /* size of array */ 113 /* This one rounds the size to integral count of segs (segments) */ 114 #define ROUNDUP(size, seg) (((size) + (seg) - 1) & ~((seg) - 1)) 115 /* Default device buffer size */ 116 #define DEF_BUFFER_SIZE 0x200 117 118 // XXX: sort up the mess in termios.h on B* ! 119 #define BLAST B230400 120 121 /* line coding defines ... Come from CDC USB specs? */ 122 #define LC_STOP_BIT_1 0 123 #define LC_STOP_BIT_2 2 124 125 #define LC_PARITY_NONE 0 126 #define LC_PARITY_ODD 1 127 #define LC_PARITY_EVEN 2 128 129 /* struct that represents line coding */ 130 typedef struct pc_serial_line_coding_s { 131 uint32 speed; 132 uint8 stopbits; 133 uint8 parity; 134 uint8 databits; 135 } pc_serial_line_coding; 136 137 /* control line states */ 138 #define CLS_LINE_DTR 0x0001 139 #define CLS_LINE_RTS 0x0002 140 141 142 143 #ifdef __BEOS__ 144 145 // All this mess is due to BeOS R5 and BONE having 146 // an incompatible module API for the same version 147 148 typedef bool (*beos_tty_service_func)(struct tty *tty, struct ddrover *rover, uint op); 149 150 // this version is compatible with BeOS R5 151 struct tty_module_info_v1_r5 { 152 // not a real bus manager... no rescan() ! 153 module_info mi; 154 status_t (*ttyopen)(struct ttyfile *, struct ddrover *, beos_tty_service_func); 155 status_t (*ttyclose)(struct ttyfile *, struct ddrover *); 156 status_t (*ttyfree)(struct ttyfile *, struct ddrover *); 157 status_t (*ttyread)(struct ttyfile *, struct ddrover *, char *, size_t *); 158 status_t (*ttywrite)(struct ttyfile *, struct ddrover *, const char *, size_t *); 159 status_t (*ttycontrol)(struct ttyfile *, struct ddrover *, ulong, void *, size_t); 160 void (*ttyinit)(struct tty *, bool); 161 void (*ttyilock)(struct tty *, struct ddrover *, bool ); 162 void (*ttyhwsignal)(struct tty *, struct ddrover *, int, bool); 163 int (*ttyin)(struct tty *, struct ddrover *, int); 164 int (*ttyout)(struct tty *, struct ddrover *); 165 struct ddrover *(*ddrstart)(struct ddrover *); 166 void (*ddrdone)(struct ddrover *); 167 void (*ddacquire)(struct ddrover *, struct ddomain *); 168 }; 169 170 // BeOS BONE has a different module with the same version... 171 struct tty_module_info_v1_bone { 172 // not a real bus manager... no rescan() ! 173 module_info mi; 174 status_t (*ttyopen)(struct ttyfile *, struct ddrover *, beos_tty_service_func); 175 status_t (*ttyclose)(struct ttyfile *, struct ddrover *); 176 status_t (*ttyfree)(struct ttyfile *, struct ddrover *); 177 status_t (*ttyread)(struct ttyfile *, struct ddrover *, char *, size_t *); 178 status_t (*ttywrite)(struct ttyfile *, struct ddrover *, const char *, size_t *); 179 status_t (*ttycontrol)(struct ttyfile *, struct ddrover *, ulong, void *, size_t); 180 status_t (*ttyselect)(struct ttyfile *, struct ddrover *, uint8, uint32, selectsync *); 181 status_t (*ttydeselect)(struct ttyfile *, struct ddrover *, uint8, selectsync *); 182 183 void (*ttyinit)(struct tty *, bool); 184 void (*ttyilock)(struct tty *, struct ddrover *, bool ); 185 void (*ttyhwsignal)(struct tty *, struct ddrover *, int, bool); 186 int (*ttyin)(struct tty *, struct ddrover *, int); 187 int (*ttyout)(struct tty *, struct ddrover *); 188 struct ddrover *(*ddrstart)(struct ddrover *); 189 void (*ddrdone)(struct ddrover *); 190 void (*ddacquire)(struct ddrover *, struct ddomain *); 191 }; 192 #endif /* __BEOS__ */ 193 194 195 extern config_manager_for_driver_module_info *gConfigManagerModule; 196 extern isa_module_info *gISAModule; 197 extern pci_module_info *gPCIModule; 198 #ifdef __HAIKU__ 199 extern tty_module_info *gTTYModule; 200 #else 201 extern tty_module_info_v1_bone *gTTYModule; 202 #endif 203 extern struct ddomain gSerialDomain; 204 205 extern "C" { 206 207 status_t init_hardware(); 208 void uninit_driver(); 209 210 #ifdef __BEOS__ 211 bool pc_serial_service(struct tty *ptty, struct ddrover *ddr, uint flags); 212 #else 213 bool pc_serial_service(struct tty *tty, uint32 op, void *buffer, 214 size_t length); 215 #endif /* __BEOS__ */ 216 int32 pc_serial_interrupt(void *arg); 217 218 status_t pc_serial_open(const char *name, uint32 flags, void **cookie); 219 status_t pc_serial_read(void *cookie, off_t position, void *buffer, size_t *numBytes); 220 status_t pc_serial_write(void *cookie, off_t position, const void *buffer, size_t *numBytes); 221 status_t pc_serial_control(void *cookie, uint32 op, void *arg, size_t length); 222 status_t pc_serial_select(void *cookie, uint8 event, uint32 ref, selectsync *sync); 223 status_t pc_serial_deselect(void *coookie, uint8 event, selectsync *sync); 224 status_t pc_serial_close(void *cookie); 225 status_t pc_serial_free(void *cookie); 226 227 const char **publish_devices(); 228 device_hooks *find_device(const char *name); 229 230 } 231 232 233 234 #endif //_PC_SERIAL_DRIVER_H_ 235