1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * -- <linux/cdrom.h>
4  * General header file for linux CD-ROM drivers
5  * Copyright (C) 1992         David Giller, rafetmad@oxy.edu
6  *               1994, 1995   Eberhard Mönkeberg, emoenke@gwdg.de
7  *               1996         David van Leeuwen, david@tm.tno.nl
8  *               1997, 1998   Erik Andersen, andersee@debian.org
9  *               1998-2002    Jens Axboe, axboe@suse.de
10  */
11 #ifndef	_LINUX_CDROM_H
12 #define	_LINUX_CDROM_H
13 
14 #include <linux/fs.h>		/* not really needed, later.. */
15 #include <linux/list.h>
16 #include <scsi/scsi_common.h>
17 #include <uapi/linux/cdrom.h>
18 
19 struct packet_command
20 {
21 	unsigned char 		cmd[CDROM_PACKET_SIZE];
22 	unsigned char 		*buffer;
23 	unsigned int 		buflen;
24 	int			stat;
25 	struct scsi_sense_hdr	*sshdr;
26 	unsigned char		data_direction;
27 	int			quiet;
28 	int			timeout;
29 	void			*reserved[1];
30 };
31 
32 /*
33  * _OLD will use PIO transfer on atapi devices, _BPC_* will use DMA
34  */
35 #define CDDA_OLD		0	/* old style */
36 #define CDDA_BPC_SINGLE		1	/* single frame block pc */
37 #define CDDA_BPC_FULL		2	/* multi frame block pc */
38 
39 /* Uniform cdrom data structures for cdrom.c */
40 struct cdrom_device_info {
41 	const struct cdrom_device_ops *ops; /* link to device_ops */
42 	struct list_head list;		/* linked list of all device_info */
43 	struct gendisk *disk;		/* matching block layer disk */
44 	void *handle;		        /* driver-dependent data */
45 /* specifications */
46 	int mask;                       /* mask of capability: disables them */
47 	int speed;			/* maximum speed for reading data */
48 	int capacity;			/* number of discs in jukebox */
49 /* device-related storage */
50 	unsigned int options	: 30;	/* options flags */
51 	unsigned mc_flags	: 2;	/* media change buffer flags */
52 	unsigned int vfs_events;	/* cached events for vfs path */
53 	unsigned int ioctl_events;	/* cached events for ioctl path */
54     	int use_count;                  /* number of times device opened */
55     	char name[20];                  /* name of the device type */
56 /* per-device flags */
57         __u8 sanyo_slot		: 2;	/* Sanyo 3 CD changer support */
58         __u8 keeplocked		: 1;	/* CDROM_LOCKDOOR status */
59         __u8 reserved		: 5;	/* not used yet */
60 	int cdda_method;		/* see flags */
61 	__u8 last_sense;
62 	__u8 media_written;		/* dirty flag, DVD+RW bookkeeping */
63 	unsigned short mmc3_profile;	/* current MMC3 profile */
64 	int for_data;
65 	int (*exit)(struct cdrom_device_info *);
66 	int mrw_mode_page;
67 	__s64 last_media_change_ms;
68 };
69 
70 struct cdrom_device_ops {
71 /* routines */
72 	int (*open) (struct cdrom_device_info *, int);
73 	void (*release) (struct cdrom_device_info *);
74 	int (*drive_status) (struct cdrom_device_info *, int);
75 	unsigned int (*check_events) (struct cdrom_device_info *cdi,
76 				      unsigned int clearing, int slot);
77 	int (*tray_move) (struct cdrom_device_info *, int);
78 	int (*lock_door) (struct cdrom_device_info *, int);
79 	int (*select_speed) (struct cdrom_device_info *, int);
80 	int (*get_last_session) (struct cdrom_device_info *,
81 				 struct cdrom_multisession *);
82 	int (*get_mcn) (struct cdrom_device_info *,
83 			struct cdrom_mcn *);
84 	/* hard reset device */
85 	int (*reset) (struct cdrom_device_info *);
86 	/* play stuff */
87 	int (*audio_ioctl) (struct cdrom_device_info *,unsigned int, void *);
88 
89 	/* handle uniform packets for scsi type devices (scsi,atapi) */
90 	int (*generic_packet) (struct cdrom_device_info *,
91 			       struct packet_command *);
92 	int (*read_cdda_bpc)(struct cdrom_device_info *cdi, void __user *ubuf,
93 			       u32 lba, u32 nframes, u8 *last_sense);
94 /* driver specifications */
95 	const int capability;   /* capability flags */
96 };
97 
98 int cdrom_multisession(struct cdrom_device_info *cdi,
99 		struct cdrom_multisession *info);
100 int cdrom_read_tocentry(struct cdrom_device_info *cdi,
101 		struct cdrom_tocentry *entry);
102 
103 /* the general block_device operations structure: */
104 extern int cdrom_open(struct cdrom_device_info *cdi, struct block_device *bdev,
105 			fmode_t mode);
106 extern void cdrom_release(struct cdrom_device_info *cdi, fmode_t mode);
107 extern int cdrom_ioctl(struct cdrom_device_info *cdi, struct block_device *bdev,
108 		       fmode_t mode, unsigned int cmd, unsigned long arg);
109 extern unsigned int cdrom_check_events(struct cdrom_device_info *cdi,
110 				       unsigned int clearing);
111 
112 extern int register_cdrom(struct gendisk *disk, struct cdrom_device_info *cdi);
113 extern void unregister_cdrom(struct cdrom_device_info *cdi);
114 
115 typedef struct {
116     int data;
117     int audio;
118     int cdi;
119     int xa;
120     long error;
121 } tracktype;
122 
123 extern int cdrom_get_last_written(struct cdrom_device_info *cdi, long *last_written);
124 extern int cdrom_number_of_slots(struct cdrom_device_info *cdi);
125 extern int cdrom_mode_select(struct cdrom_device_info *cdi,
126 			     struct packet_command *cgc);
127 extern int cdrom_mode_sense(struct cdrom_device_info *cdi,
128 			    struct packet_command *cgc,
129 			    int page_code, int page_control);
130 extern void init_cdrom_command(struct packet_command *cgc,
131 			       void *buffer, int len, int type);
132 extern int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
133 				      struct packet_command *cgc);
134 
135 /* The SCSI spec says there could be 256 slots. */
136 #define CDROM_MAX_SLOTS	256
137 
138 struct cdrom_mechstat_header {
139 #if defined(__BIG_ENDIAN_BITFIELD)
140 	__u8 fault         : 1;
141 	__u8 changer_state : 2;
142 	__u8 curslot       : 5;
143 	__u8 mech_state    : 3;
144 	__u8 door_open     : 1;
145 	__u8 reserved1     : 4;
146 #elif defined(__LITTLE_ENDIAN_BITFIELD)
147 	__u8 curslot       : 5;
148 	__u8 changer_state : 2;
149 	__u8 fault         : 1;
150 	__u8 reserved1     : 4;
151 	__u8 door_open     : 1;
152 	__u8 mech_state    : 3;
153 #endif
154 	__u8     curlba[3];
155 	__u8     nslots;
156 	__u16 slot_tablelen;
157 };
158 
159 struct cdrom_slot {
160 #if defined(__BIG_ENDIAN_BITFIELD)
161 	__u8 disc_present : 1;
162 	__u8 reserved1    : 6;
163 	__u8 change       : 1;
164 #elif defined(__LITTLE_ENDIAN_BITFIELD)
165 	__u8 change       : 1;
166 	__u8 reserved1    : 6;
167 	__u8 disc_present : 1;
168 #endif
169 	__u8 reserved2[3];
170 };
171 
172 struct cdrom_changer_info {
173 	struct cdrom_mechstat_header hdr;
174 	struct cdrom_slot slots[CDROM_MAX_SLOTS];
175 };
176 
177 typedef enum {
178 	mechtype_caddy = 0,
179 	mechtype_tray  = 1,
180 	mechtype_popup = 2,
181 	mechtype_individual_changer = 4,
182 	mechtype_cartridge_changer  = 5
183 } mechtype_t;
184 
185 typedef struct {
186 #if defined(__BIG_ENDIAN_BITFIELD)
187 	__u8 ps			: 1;
188 	__u8 reserved1		: 1;
189 	__u8 page_code		: 6;
190         __u8 page_length;
191 	__u8 reserved2		: 1;
192 	__u8 bufe		: 1;
193 	__u8 ls_v		: 1;
194 	__u8 test_write		: 1;
195         __u8 write_type		: 4;
196 	__u8 multi_session	: 2; /* or border, DVD */
197 	__u8 fp			: 1;
198 	__u8 copy		: 1;
199 	__u8 track_mode		: 4;
200 	__u8 reserved3		: 4;
201 	__u8 data_block_type	: 4;
202 #elif defined(__LITTLE_ENDIAN_BITFIELD)
203 	__u8 page_code		: 6;
204 	__u8 reserved1		: 1;
205 	__u8 ps			: 1;
206         __u8 page_length;
207         __u8 write_type		: 4;
208 	__u8 test_write		: 1;
209 	__u8 ls_v		: 1;
210 	__u8 bufe		: 1;
211 	__u8 reserved2		: 1;
212 	__u8 track_mode		: 4;
213 	__u8 copy		: 1;
214 	__u8 fp			: 1;
215 	__u8 multi_session	: 2; /* or border, DVD */
216 	__u8 data_block_type	: 4;
217 	__u8 reserved3		: 4;
218 #endif
219 	__u8 link_size;
220 	__u8 reserved4;
221 #if defined(__BIG_ENDIAN_BITFIELD)
222 	__u8 reserved5		: 2;
223 	__u8 app_code		: 6;
224 #elif defined(__LITTLE_ENDIAN_BITFIELD)
225 	__u8 app_code		: 6;
226 	__u8 reserved5		: 2;
227 #endif
228 	__u8 session_format;
229 	__u8 reserved6;
230 	__be32 packet_size;
231 	__u16 audio_pause;
232 	__u8 mcn[16];
233 	__u8 isrc[16];
234 	__u8 subhdr0;
235 	__u8 subhdr1;
236 	__u8 subhdr2;
237 	__u8 subhdr3;
238 } __attribute__((packed)) write_param_page;
239 
240 struct modesel_head
241 {
242 	__u8	reserved1;
243 	__u8	medium;
244 	__u8	reserved2;
245 	__u8	block_desc_length;
246 	__u8	density;
247 	__u8	number_of_blocks_hi;
248 	__u8	number_of_blocks_med;
249 	__u8	number_of_blocks_lo;
250 	__u8	reserved3;
251 	__u8	block_length_hi;
252 	__u8	block_length_med;
253 	__u8	block_length_lo;
254 };
255 
256 typedef struct {
257 	__u16 report_key_length;
258 	__u8 reserved1;
259 	__u8 reserved2;
260 #if defined(__BIG_ENDIAN_BITFIELD)
261 	__u8 type_code			: 2;
262 	__u8 vra			: 3;
263 	__u8 ucca			: 3;
264 #elif defined(__LITTLE_ENDIAN_BITFIELD)
265 	__u8 ucca			: 3;
266 	__u8 vra			: 3;
267 	__u8 type_code			: 2;
268 #endif
269 	__u8 region_mask;
270 	__u8 rpc_scheme;
271 	__u8 reserved3;
272 } rpc_state_t;
273 
274 struct event_header {
275 	__be16 data_len;
276 #if defined(__BIG_ENDIAN_BITFIELD)
277 	__u8 nea		: 1;
278 	__u8 reserved1		: 4;
279 	__u8 notification_class	: 3;
280 #elif defined(__LITTLE_ENDIAN_BITFIELD)
281 	__u8 notification_class	: 3;
282 	__u8 reserved1		: 4;
283 	__u8 nea		: 1;
284 #endif
285 	__u8 supp_event_class;
286 };
287 
288 struct media_event_desc {
289 #if defined(__BIG_ENDIAN_BITFIELD)
290 	__u8 reserved1		: 4;
291 	__u8 media_event_code	: 4;
292 	__u8 reserved2		: 6;
293 	__u8 media_present	: 1;
294 	__u8 door_open		: 1;
295 #elif defined(__LITTLE_ENDIAN_BITFIELD)
296 	__u8 media_event_code	: 4;
297 	__u8 reserved1		: 4;
298 	__u8 door_open		: 1;
299 	__u8 media_present	: 1;
300 	__u8 reserved2		: 6;
301 #endif
302 	__u8 start_slot;
303 	__u8 end_slot;
304 };
305 
306 extern int cdrom_get_media_event(struct cdrom_device_info *cdi, struct media_event_desc *med);
307 
lba_to_msf(int lba,u8 * m,u8 * s,u8 * f)308 static inline void lba_to_msf(int lba, u8 *m, u8 *s, u8 *f)
309 {
310 	lba += CD_MSF_OFFSET;
311 	lba &= 0xffffff;  /* negative lbas use only 24 bits */
312 	*m = lba / (CD_SECS * CD_FRAMES);
313 	lba %= (CD_SECS * CD_FRAMES);
314 	*s = lba / CD_FRAMES;
315 	*f = lba % CD_FRAMES;
316 }
317 
msf_to_lba(u8 m,u8 s,u8 f)318 static inline int msf_to_lba(u8 m, u8 s, u8 f)
319 {
320 	return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
321 }
322 #endif  /* _LINUX_CDROM_H */
323