xref: /haiku/headers/os/drivers/Drivers.h (revision 7120e97489acbf17d86d3f33e3b2e68974fd4b23)
1 #ifndef _DRIVERS_DRIVERS_H
2 #define _DRIVERS_DRIVERS_H
3 
4 #include <BeBuild.h>
5 #include <sys/types.h>
6 #include <sys/uio.h>
7 #include <SupportDefs.h>
8 
9 /* ---
10 	these hooks are how the kernel accesses the device
11 --- */
12 
13 // XXX hack, this is also in private/kernel/defines.h
14 #ifndef SYS_MAX_PATH_LEN
15  #define SYS_MAX_PATH_LEN 512
16 #endif
17 
18 struct selectsync;
19 typedef struct selectsync selectsync;
20 
21 typedef status_t (*device_open_hook) (const char *name, uint32 flags, void **cookie);
22 typedef status_t (*device_close_hook) (void *cookie);
23 typedef status_t (*device_free_hook) (void *cookie);
24 typedef status_t (*device_control_hook) (void *cookie, uint32 op, void *data,
25                                          size_t len);
26 typedef ssize_t  (*device_read_hook) (void *cookie, off_t position, void *data,
27                                       size_t *numBytes);
28 typedef ssize_t  (*device_write_hook) (void *cookie, off_t position,
29                                        const void *data, size_t *numBytes);
30 typedef status_t (*device_select_hook) (void *cookie, uint8 event, uint32 ref,
31                                         selectsync *sync);
32 typedef status_t (*device_deselect_hook) (void *cookie, uint8 event,
33                                           selectsync *sync);
34 typedef status_t (*device_readv_hook) (void *cookie, off_t position, const iovec *vec,
35 					size_t count, size_t *numBytes);
36 typedef status_t (*device_writev_hook) (void *cookie, off_t position, const iovec *vec,
37 					size_t count, size_t *numBytes);
38 
39 #define	B_CUR_DRIVER_API_VERSION	2
40 
41 /* ---
42 	the device_hooks structure is a descriptor for the device, giving its
43 	entry points.
44 --- */
45 
46 typedef struct {
47 	device_open_hook		open;		/* called to open the device */
48 	device_close_hook		close;		/* called to close the device */
49 	device_free_hook		free;		/* called to free the cookie */
50 	device_control_hook		control;	/* called to control the device */
51 	device_read_hook		read;		/* reads from the device */
52 	device_write_hook		write;		/* writes to the device */
53 	device_select_hook		select;		/* start select */
54 	device_deselect_hook	deselect;	/* stop select */
55 	device_readv_hook		readv;		/* scatter-gather read from the device */
56 	device_writev_hook		writev;		/* scatter-gather write to the device */
57 } device_hooks;
58 
59 status_t		init_hardware(void);
60 const char	  **publish_devices(void);
61 device_hooks	*find_device(const char *name);
62 status_t		init_driver(void);
63 void			uninit_driver(void);
64 
65 extern int32	api_version;
66 
67 enum {
68 	// called on partition device to get info
69 	IOCTL_DEVFS_GET_PARTITION_INFO = 1000,
70 	// called on raw device to declare one partition
71 	IOCTL_DEVFS_SET_PARTITION,
72 };
73 
74 typedef struct devfs_partition_info {
75 	// offset and size relative to raw device in bytes
76 	off_t offset;
77 	off_t size;
78 
79 	// logical block size in bytes
80 	uint32 logical_block_size;
81 
82 	// session/partition id
83 	uint32 session;
84 	uint32 partition;
85 
86 	// path of raw device (GET_PARTITION_INFO only)
87 	char raw_device[SYS_MAX_PATH_LEN];
88 } devfs_partition_info;
89 
90 enum {
91 	B_GET_DEVICE_SIZE = 1,			/* get # bytes */
92 									/*   returns size_t in *data */
93 
94 	B_SET_DEVICE_SIZE,				/* set # bytes */
95 									/*   passed size_t in *data */
96 
97 	B_SET_NONBLOCKING_IO,			/* set to non-blocking i/o */
98 
99 	B_SET_BLOCKING_IO,				/* set to blocking i/o */
100 
101 	B_GET_READ_STATUS,				/* check if can read w/o blocking */
102 									/*   returns bool in *data */
103 
104 	B_GET_WRITE_STATUS,				/* check if can write w/o blocking */
105 									/*   returns bool in *data */
106 
107 	B_GET_GEOMETRY,					/* get info about device geometry */
108 									/*   returns struct geometry in *data */
109 
110 	B_GET_DRIVER_FOR_DEVICE,		/* get the path of the executable serving that device */
111 
112 	B_GET_PARTITION_INFO,			/* get info about a device partition */
113 									/*   returns struct partition_info in *data */
114 
115 	B_SET_PARTITION,				/* create a user-defined partition */
116 
117 	B_FORMAT_DEVICE,				/* low-level device format */
118 
119 	B_EJECT_DEVICE,					/* eject the media if supported */
120 
121 	B_GET_ICON,						/* return device icon (see struct below) */
122 
123 	B_GET_BIOS_GEOMETRY,			/* get info about device geometry */
124 									/* as reported by the bios */
125 									/*   returns struct geometry in *data */
126 
127 	B_GET_MEDIA_STATUS,				/* get status of media. */
128 									/* return status_t in *data: */
129 									/* B_NO_ERROR: media ready */
130 									/* B_DEV_NO_MEDIA: no media */
131 									/* B_DEV_NOT_READY: device not ready */
132 									/* B_DEV_MEDIA_CHANGED: media changed */
133 									/*  since open or last B_GET_MEDIA_STATUS */
134 									/* B_DEV_MEDIA_CHANGE_REQUESTED: user */
135 									/*  pressed button on drive */
136 									/* B_DEV_DOOR_OPEN: door open */
137 
138 	B_LOAD_MEDIA,					/* load the media if supported */
139 
140 	B_GET_BIOS_DRIVE_ID,			/* get bios id for this device */
141 
142 	B_SET_UNINTERRUPTABLE_IO,		/* prevent cntl-C from interrupting i/o */
143 	B_SET_INTERRUPTABLE_IO,			/* allow cntl-C to interrupt i/o */
144 
145 	B_FLUSH_DRIVE_CACHE,			/* flush drive cache */
146 
147 	B_GET_NEXT_OPEN_DEVICE = 1000,	/* iterate through open devices */
148 	B_ADD_FIXED_DRIVER,				/* private */
149 	B_REMOVE_FIXED_DRIVER,			/* private */
150 
151 	B_AUDIO_DRIVER_BASE = 8000,		/* base for codes in audio_driver.h */
152 	B_MIDI_DRIVER_BASE = 8100,		/* base for codes in midi_driver.h */
153 	B_JOYSTICK_DRIVER_BASE = 8200,	/* base for codes in joystick.h */
154 	B_GRAPHIC_DRIVER_BASE = 8300,	/* base for codes in graphic_driver.h */
155 
156 	B_DEVICE_OP_CODES_END = 9999	/* end of Be-defined contol id's */
157 };
158 
159 /* ---
160 	geometry structure for the B_GET_GEOMETRY opcode
161 --- */
162 
163 typedef struct {
164 	uint32	bytes_per_sector;		/* sector size in bytes */
165 	uint32	sectors_per_track;		/* # sectors per track */
166 	uint32	cylinder_count;			/* # cylinders */
167 	uint32	head_count;				/* # heads */
168 	uchar	device_type;			/* type */
169 	bool	removable;				/* non-zero if removable */
170 	bool	read_only;				/* non-zero if read only */
171 	bool	write_once;				/* non-zero if write-once */
172 } device_geometry;
173 
174 
175 /* ---
176 	Be-defined device types returned by B_GET_GEOMETRY.  Use these if it makes
177 	sense for your device.
178 --- */
179 
180 enum {
181 	B_DISK = 0,						/* Hard disks, floppy disks, etc. */
182 	B_TAPE,							/* Tape drives */
183 	B_PRINTER,						/* Printers */
184 	B_CPU,							/* CPU devices */
185 	B_WORM,							/* Write-once, read-many devives */
186 	B_CD,							/* CD ROMS */
187 	B_SCANNER,						/* Scanners */
188 	B_OPTICAL,						/* Optical devices */
189 	B_JUKEBOX,						/* Jukeboxes */
190 	B_NETWORK						/* Network devices */
191 };
192 
193 
194 /* ---
195 	partition_info structure used by B_GET_PARTITION_INFO and B_SET_PARTITION
196 --- */
197 
198 typedef struct {
199 	off_t	offset;					/* offset (in bytes) */
200 	off_t	size;					/* size (in bytes) */
201 	int32	logical_block_size;		/* logical block size of partition */
202 	int32	session;				/* id of session */
203 	int32	partition;				/* id of partition */
204 	char	device[256];			/* path to the physical device */
205 } partition_info;
206 
207 /* ---
208 	driver_path structure returned by the B_GET_DRIVER_FOR_DEVICE
209 --- */
210 
211 typedef char	driver_path[256];
212 
213 
214 /* ---
215 	open_device_iterator structure used by the B_GET_NEXT_OPEN_DEVICE opcode
216 --- */
217 
218 typedef struct {
219 	uint32		cookie;			/* must be set to 0 before iterating */
220 	char		device[256];	/* device path */
221 } open_device_iterator;
222 
223 
224 /* ---
225 	icon structure for the B_GET_ICON opcode
226 --- */
227 
228 typedef struct {
229 	int32	icon_size;			/* icon size requested */
230 	void	*icon_data;			/* where to put 'em (usually BBitmap->Bits()) */
231 } device_icon;
232 
233 
234 
235 #endif /* _DRIVERS_DRIVERS_H */
236