Bug Summary

File:scsi/cd.c
Warning:line 335, column 5
Value stored to 'error' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name cd.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -ffreestanding -mcmodel=kernel -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -target-feature -sse2 -target-feature -sse -target-feature -3dnow -target-feature -mmx -target-feature +save-args -target-feature +retpoline-external-thunk -disable-red-zone -no-implicit-float -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -nostdsysteminc -nobuiltininc -resource-dir /usr/local/llvm16/lib/clang/16 -I /usr/src/sys -I /usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -I /usr/src/sys/arch -I /usr/src/sys/dev/pci/drm/include -I /usr/src/sys/dev/pci/drm/include/uapi -I /usr/src/sys/dev/pci/drm/amd/include/asic_reg -I /usr/src/sys/dev/pci/drm/amd/include -I /usr/src/sys/dev/pci/drm/amd/amdgpu -I /usr/src/sys/dev/pci/drm/amd/display -I /usr/src/sys/dev/pci/drm/amd/display/include -I /usr/src/sys/dev/pci/drm/amd/display/dc -I /usr/src/sys/dev/pci/drm/amd/display/amdgpu_dm -I /usr/src/sys/dev/pci/drm/amd/pm/inc -I /usr/src/sys/dev/pci/drm/amd/pm/legacy-dpm -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu11 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu12 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu13 -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/inc -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/hwmgr -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/smumgr -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc/pmfw_if -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc/hw -I /usr/src/sys/dev/pci/drm/amd/display/dc/clk_mgr -I /usr/src/sys/dev/pci/drm/amd/display/modules/inc -I /usr/src/sys/dev/pci/drm/amd/display/modules/hdcp -I /usr/src/sys/dev/pci/drm/amd/display/dmub/inc -I /usr/src/sys/dev/pci/drm/i915 -D DDB -D DIAGNOSTIC -D KTRACE -D ACCOUNTING -D KMEMSTATS -D PTRACE -D POOL_DEBUG -D CRYPTO -D SYSVMSG -D SYSVSEM -D SYSVSHM -D UVM_SWAP_ENCRYPT -D FFS -D FFS2 -D FFS_SOFTUPDATES -D UFS_DIRHASH -D QUOTA -D EXT2FS -D MFS -D NFSCLIENT -D NFSSERVER -D CD9660 -D UDF -D MSDOSFS -D FIFO -D FUSE -D SOCKET_SPLICE -D TCP_ECN -D TCP_SIGNATURE -D INET6 -D IPSEC -D PPP_BSDCOMP -D PPP_DEFLATE -D PIPEX -D MROUTING -D MPLS -D BOOT_CONFIG -D USER_PCICONF -D APERTURE -D MTRR -D NTFS -D SUSPEND -D HIBERNATE -D PCIVERBOSE -D USBVERBOSE -D WSDISPLAY_COMPAT_USL -D WSDISPLAY_COMPAT_RAWKBD -D WSDISPLAY_DEFAULTSCREENS=6 -D X86EMU -D ONEWIREVERBOSE -D MULTIPROCESSOR -D MAXUSERS=80 -D _KERNEL -O2 -Wno-pointer-sign -Wno-address-of-packed-member -Wno-constant-conversion -Wno-unused-but-set-variable -Wno-gnu-folding-constant -fdebug-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /home/ben/Projects/scan/2024-01-11-110808-61670-1 -x c /usr/src/sys/scsi/cd.c
1/* $OpenBSD: cd.c,v 1.266 2022/09/01 13:45:27 krw Exp $ */
2/* $NetBSD: cd.c,v 1.100 1997/04/02 02:29:30 mycroft Exp $ */
3
4/*
5 * Copyright (c) 1994, 1995, 1997 Charles M. Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Charles M. Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Originally written by Julian Elischer (julian@tfs.com)
35 * for TRW Financial Systems for use under the MACH(2.5) operating system.
36 *
37 * TRW Financial Systems, in accordance with their agreement with Carnegie
38 * Mellon University, makes this software available to CMU to distribute
39 * or use in any manner that they see fit as long as this message is kept with
40 * the software. For this reason TFS also grants any other persons or
41 * organisations permission to use or modify this software.
42 *
43 * TFS supplies this software to be publicly redistributed
44 * on the understanding that TFS is not responsible for the correct
45 * functioning of this software in any circumstances.
46 *
47 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
48 */
49
50#include <sys/param.h>
51#include <sys/systm.h>
52#include <sys/timeout.h>
53#include <sys/fcntl.h>
54#include <sys/stat.h>
55#include <sys/ioctl.h>
56#include <sys/mtio.h>
57#include <sys/buf.h>
58#include <sys/uio.h>
59#include <sys/malloc.h>
60#include <sys/pool.h>
61#include <sys/errno.h>
62#include <sys/device.h>
63#include <sys/disklabel.h>
64#include <sys/disk.h>
65#include <sys/cdio.h>
66#include <sys/conf.h>
67#include <sys/scsiio.h>
68#include <sys/dkio.h>
69#include <sys/vnode.h>
70
71#include <scsi/scsi_all.h>
72#include <scsi/cd.h>
73#include <scsi/scsi_debug.h>
74#include <scsi/scsi_disk.h> /* rw_10 and start_stop come from there */
75#include <scsi/scsiconf.h>
76
77
78#include <ufs/ffs/fs.h> /* for BBSIZE and SBSIZE */
79
80#define CDOUTSTANDING4 4
81
82#define MAXTRACK99 99
83#define CD_FRAMES75 75
84#define CD_SECS60 60
85
86struct cd_toc {
87 struct ioc_toc_header header;
88 struct cd_toc_entry entries[MAXTRACK99+1]; /* One extra for the */
89 /* leadout */
90};
91
92int cdmatch(struct device *, void *, void *);
93void cdattach(struct device *, struct device *, void *);
94int cdactivate(struct device *, int);
95int cddetach(struct device *, int);
96
97struct cd_softc {
98 struct device sc_dev;
99 struct disk sc_dk;
100
101 int sc_flags;
102#define CDF_DYING0x40 0x40 /* dying, when deactivated */
103 struct scsi_link *sc_link; /* contains targ, lun, etc. */
104 struct cd_parms {
105 u_int32_t secsize;
106 u_int64_t disksize; /* total number sectors */
107 } params;
108 struct bufq sc_bufq;
109 struct scsi_xshandler sc_xsh;
110};
111
112void cdstart(struct scsi_xfer *);
113void cd_buf_done(struct scsi_xfer *);
114int cd_cmd_rw6(struct scsi_generic *, int, u_int64_t, u_int32_t);
115int cd_cmd_rw10(struct scsi_generic *, int, u_int64_t, u_int32_t);
116int cd_cmd_rw12(struct scsi_generic *, int, u_int64_t, u_int32_t);
117void cdminphys(struct buf *);
118int cdgetdisklabel(dev_t, struct cd_softc *, struct disklabel *, int);
119int cd_setchan(struct cd_softc *, int, int, int, int, int);
120int cd_getvol(struct cd_softc *cd, struct ioc_vol *, int);
121int cd_setvol(struct cd_softc *, const struct ioc_vol *, int);
122int cd_load_unload(struct cd_softc *, int, int);
123int cd_set_pa_immed(struct cd_softc *, int);
124int cd_play(struct cd_softc *, int, int);
125int cd_play_tracks(struct cd_softc *, int, int, int, int);
126int cd_play_msf(struct cd_softc *, int, int, int, int, int, int);
127int cd_pause(struct cd_softc *, int);
128int cd_reset(struct cd_softc *);
129int cd_read_subchannel(struct cd_softc *, int, int, int,
130 struct cd_sub_channel_info *, int );
131int cd_read_toc(struct cd_softc *, int, int, void *, int, int);
132int cd_get_parms(struct cd_softc *, int);
133int cd_load_toc(struct cd_softc *, struct cd_toc *, int);
134int cd_interpret_sense(struct scsi_xfer *);
135u_int64_t cd_size(struct scsi_link *, int, u_int32_t *);
136
137int dvd_auth(struct cd_softc *, union dvd_authinfo *);
138int dvd_read_physical(struct cd_softc *, union dvd_struct *);
139int dvd_read_copyright(struct cd_softc *, union dvd_struct *);
140int dvd_read_disckey(struct cd_softc *, union dvd_struct *);
141int dvd_read_bca(struct cd_softc *, union dvd_struct *);
142int dvd_read_manufact(struct cd_softc *, union dvd_struct *);
143int dvd_read_struct(struct cd_softc *, union dvd_struct *);
144
145#if defined(__macppc__)
146int cd_eject(void);
147#endif /* __macppc__ */
148
149const struct cfattach cd_ca = {
150 sizeof(struct cd_softc), cdmatch, cdattach,
151 cddetach, cdactivate
152};
153
154struct cfdriver cd_cd = {
155 NULL((void *)0), "cd", DV_DISK
156};
157
158const struct scsi_inquiry_pattern cd_patterns[] = {
159 {T_CDROM0x05, T_REMOV1,
160 "", "", ""},
161 {T_CDROM0x05, T_FIXED0,
162 "", "", ""},
163 {T_WORM0x04, T_REMOV1,
164 "", "", ""},
165 {T_WORM0x04, T_FIXED0,
166 "", "", ""},
167 {T_DIRECT0x00, T_REMOV1,
168 "NEC CD-ROM DRIVE:260", "", ""},
169#if 0
170 {T_CDROM0x05, T_REMOV1, /* more luns */
171 "PIONEER ", "CD-ROM DRM-600 ", ""},
172#endif /* 0 */
173};
174
175#define cdlookup(unit)(struct cd_softc *)disk_lookup(&cd_cd, (unit)) (struct cd_softc *)disk_lookup(&cd_cd, (unit))
176
177int
178cdmatch(struct device *parent, void *match, void *aux)
179{
180 struct scsi_attach_args *sa = aux;
181 struct scsi_inquiry_data *inq = &sa->sa_sc_link->inqdata;
182 int priority;
183
184 scsi_inqmatch(inq, cd_patterns, nitems(cd_patterns)(sizeof((cd_patterns)) / sizeof((cd_patterns)[0])),
185 sizeof(cd_patterns[0]), &priority);
186
187 return priority;
188}
189
190/*
191 * The routine called by the low level scsi routine when it discovers
192 * A device suitable for this driver
193 */
194void
195cdattach(struct device *parent, struct device *self, void *aux)
196{
197 struct cd_softc *sc = (struct cd_softc *)self;
198 struct scsi_attach_args *sa = aux;
199 struct scsi_link *link = sa->sa_sc_link;
200
201 SC_DEBUG(link, SDEV_DB2, ("cdattach:\n"));
202
203 /*
204 * Store information needed to contact our base driver
205 */
206 sc->sc_link = link;
207 link->interpret_sense = cd_interpret_sense;
208 link->device_softc = sc;
209 if (link->openings > CDOUTSTANDING4)
210 link->openings = CDOUTSTANDING4;
211
212 /*
213 * Initialize disk structures.
214 */
215 sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
216 bufq_init(&sc->sc_bufq, BUFQ_DEFAULT1);
217
218 printf("\n");
219
220 scsi_xsh_set(&sc->sc_xsh, link, cdstart);
221
222 /* Attach disk. */
223 sc->sc_dk.dk_flags = DKF_NOLABELREAD0x0004;
224 disk_attach(&sc->sc_dev, &sc->sc_dk);
225}
226
227
228int
229cdactivate(struct device *self, int act)
230{
231 struct cd_softc *sc = (struct cd_softc *)self;
232
233 switch (act) {
234 case DVACT_RESUME4:
235 /*
236 * When resuming, hardware may have forgotten we locked it. So
237 * if there are any open partitions, lock the CD.
238 */
239 if (sc->sc_dk.dk_openmask != 0)
240 scsi_prevent(sc->sc_link, PR_PREVENT0x01,
241 SCSI_IGNORE_ILLEGAL_REQUEST0x00100 |
242 SCSI_IGNORE_MEDIA_CHANGE0x00080 |
243 SCSI_SILENT0x00020 | SCSI_AUTOCONF0x00003);
244 break;
245 case DVACT_DEACTIVATE1:
246 SET(sc->sc_flags, CDF_DYING)((sc->sc_flags) |= (0x40));
247 scsi_xsh_del(&sc->sc_xsh);
248 break;
249 }
250 return 0;
251}
252
253int
254cddetach(struct device *self, int flags)
255{
256 struct cd_softc *sc = (struct cd_softc *)self;
257
258 bufq_drain(&sc->sc_bufq);
259
260 disk_gone(cdopen, self->dv_unit);
261
262 /* Detach disk. */
263 bufq_destroy(&sc->sc_bufq);
264 disk_detach(&sc->sc_dk);
265
266 return 0;
267}
268
269/*
270 * Open the device. Make sure the partition info is as up-to-date as can be.
271 */
272int
273cdopen(dev_t dev, int flag, int fmt, struct proc *p)
274{
275 struct scsi_link *link;
276 struct cd_softc *sc;
277 int error = 0, part, rawopen, unit;
278
279 unit = DISKUNIT(dev)(((unsigned)((dev) & 0xff) | (((dev) & 0xffff0000) >>
8)) / 16)
;
280 part = DISKPART(dev)(((unsigned)((dev) & 0xff) | (((dev) & 0xffff0000) >>
8)) % 16)
;
281
282 rawopen = (part == RAW_PART2) && (fmt == S_IFCHR0020000);
283
284 sc = cdlookup(unit)(struct cd_softc *)disk_lookup(&cd_cd, (unit));
285 if (sc == NULL((void *)0))
286 return ENXIO6;
287 if (ISSET(sc->sc_flags, CDF_DYING)((sc->sc_flags) & (0x40))) {
288 device_unref(&sc->sc_dev);
289 return ENXIO6;
290 }
291
292 link = sc->sc_link;
293 SC_DEBUG(link, SDEV_DB1,
294 ("cdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
295 cd_cd.cd_ndevs, part));
296
297 if ((error = disk_lock(&sc->sc_dk)) != 0) {
298 device_unref(&sc->sc_dev);
299 return error;
300 }
301
302 if (sc->sc_dk.dk_openmask != 0) {
303 /*
304 * If any partition is open, but the disk has been invalidated,
305 * disallow further opens.
306 */
307 if (!ISSET(link->flags, SDEV_MEDIA_LOADED)((link->flags) & (0x0002))) {
308 if (rawopen)
309 goto out;
310 error = EIO5;
311 goto bad;
312 }
313 } else {
314 /*
315 * Check that it is still responding and ok. Drive can be in
316 * progress of loading media so use increased retries number
317 * and don't ignore NOT_READY.
318 */
319
320 /* Use cd_interpret_sense() now. */
321 SET(link->flags, SDEV_OPEN)((link->flags) |= (0x0008));
322
323 error = scsi_test_unit_ready(link, TEST_READY_RETRIES5,
324 (rawopen ? SCSI_SILENT0x00020 : 0) | SCSI_IGNORE_ILLEGAL_REQUEST0x00100 |
325 SCSI_IGNORE_MEDIA_CHANGE0x00080);
326
327 /* Start the cd spinning if necessary. */
328 if (error == EIO5)
329 error = scsi_start(link, SSS_START0x01,
330 SCSI_IGNORE_ILLEGAL_REQUEST0x00100 |
331 SCSI_IGNORE_MEDIA_CHANGE0x00080 | SCSI_SILENT0x00020);
332
333 if (error) {
334 if (rawopen) {
335 error = 0;
Value stored to 'error' is never read
336 goto out;
337 } else
338 goto bad;
339 }
340
341 /* Lock the cd in. */
342 error = scsi_prevent(link, PR_PREVENT0x01,
343 SCSI_IGNORE_ILLEGAL_REQUEST0x00100 | SCSI_IGNORE_MEDIA_CHANGE0x00080 |
344 SCSI_SILENT0x00020);
345 if (error)
346 goto bad;
347
348 /* Load the physical device parameters. */
349 SET(link->flags, SDEV_MEDIA_LOADED)((link->flags) |= (0x0002));
350 if (cd_get_parms(sc, (rawopen ? SCSI_SILENT0x00020 : 0) |
351 SCSI_IGNORE_ILLEGAL_REQUEST0x00100 | SCSI_IGNORE_MEDIA_CHANGE0x00080)) {
352 CLR(link->flags, SDEV_MEDIA_LOADED)((link->flags) &= ~(0x0002));
353 error = ENXIO6;
354 goto bad;
355 }
356 SC_DEBUG(link, SDEV_DB3, ("Params loaded\n"));
357
358 /* Fabricate a disk label. */
359 cdgetdisklabel(dev, sc, sc->sc_dk.dk_label, 0);
360 SC_DEBUG(link, SDEV_DB3, ("Disklabel fabricated\n"));
361 }
362
363out:
364 if ((error = disk_openpart(&sc->sc_dk, part, fmt, 1)) != 0)
365 goto bad;
366
367 SET(link->flags, SDEV_OPEN)((link->flags) |= (0x0008));
368 SC_DEBUG(link, SDEV_DB3, ("open complete\n"));
369
370 /* It's OK to fall through because dk_openmask is now non-zero. */
371bad:
372 if (sc->sc_dk.dk_openmask == 0) {
373 scsi_prevent(link, PR_ALLOW0x00,
374 SCSI_IGNORE_ILLEGAL_REQUEST0x00100 | SCSI_IGNORE_MEDIA_CHANGE0x00080 |
375 SCSI_SILENT0x00020);
376 CLR(link->flags, SDEV_OPEN | SDEV_MEDIA_LOADED)((link->flags) &= ~(0x0008 | 0x0002));
377 }
378
379 disk_unlock(&sc->sc_dk);
380 device_unref(&sc->sc_dev);
381 return error;
382}
383
384/*
385 * Close the device. Only called if we are the last occurrence of an open
386 * device.
387 */
388int
389cdclose(dev_t dev, int flag, int fmt, struct proc *p)
390{
391 struct cd_softc *sc;
392 int part = DISKPART(dev)(((unsigned)((dev) & 0xff) | (((dev) & 0xffff0000) >>
8)) % 16)
;
393
394 sc = cdlookup(DISKUNIT(dev))(struct cd_softc *)disk_lookup(&cd_cd, ((((unsigned)((dev
) & 0xff) | (((dev) & 0xffff0000) >> 8)) / 16))
)
;
395 if (sc == NULL((void *)0))
396 return ENXIO6;
397 if (ISSET(sc->sc_flags, CDF_DYING)((sc->sc_flags) & (0x40))) {
398 device_unref(&sc->sc_dev);
399 return ENXIO6;
400 }
401
402 disk_lock_nointr(&sc->sc_dk);
403
404 disk_closepart(&sc->sc_dk, part, fmt);
405
406 if (sc->sc_dk.dk_openmask == 0) {
407 /* XXXX Must wait for I/O to complete! */
408
409 scsi_prevent(sc->sc_link, PR_ALLOW0x00,
410 SCSI_IGNORE_ILLEGAL_REQUEST0x00100 | SCSI_IGNORE_NOT_READY0x00040 |
411 SCSI_SILENT0x00020);
412 CLR(sc->sc_link->flags, SDEV_OPEN | SDEV_MEDIA_LOADED)((sc->sc_link->flags) &= ~(0x0008 | 0x0002));
413
414 if (ISSET(sc->sc_link->flags, SDEV_EJECTING)((sc->sc_link->flags) & (0x0100))) {
415 scsi_start(sc->sc_link, SSS_STOP0x00|SSS_LOEJ0x02, 0);
416
417 CLR(sc->sc_link->flags, SDEV_EJECTING)((sc->sc_link->flags) &= ~(0x0100));
418 }
419
420 scsi_xsh_del(&sc->sc_xsh);
421 }
422
423 disk_unlock(&sc->sc_dk);
424
425 device_unref(&sc->sc_dev);
426 return 0;
427}
428
429/*
430 * Actually translate the requested transfer into one the physical driver can
431 * understand. The transfer is described by a buf and will include only one
432 * physical transfer.
433 */
434void
435cdstrategy(struct buf *bp)
436{
437 struct cd_softc *sc;
438 int s;
439
440 sc = cdlookup(DISKUNIT(bp->b_dev))(struct cd_softc *)disk_lookup(&cd_cd, ((((unsigned)((bp->
b_dev) & 0xff) | (((bp->b_dev) & 0xffff0000) >>
8)) / 16)))
;
441 if (sc == NULL((void *)0)) {
442 bp->b_error = ENXIO6;
443 goto bad;
444 }
445 if (ISSET(sc->sc_flags, CDF_DYING)((sc->sc_flags) & (0x40))) {
446 bp->b_error = ENXIO6;
447 goto bad;
448 }
449
450 SC_DEBUG(sc->sc_link, SDEV_DB2, ("cdstrategy: %ld bytes @ blk %lld\n",
451 bp->b_bcount, (long long)bp->b_blkno));
452 /*
453 * If the device has been made invalid, error out
454 * maybe the media changed, or no media loaded
455 */
456 if (!ISSET(sc->sc_link->flags, SDEV_MEDIA_LOADED)((sc->sc_link->flags) & (0x0002))) {
457 bp->b_error = EIO5;
458 goto bad;
459 }
460
461 /* Validate the request. */
462 if (bounds_check_with_label(bp, sc->sc_dk.dk_label) == -1)
463 goto done;
464
465 /* Place it in the queue of disk activities for this disk. */
466 bufq_queue(&sc->sc_bufq, bp);
467
468 /*
469 * Tell the device to get going on the transfer if it's
470 * not doing anything, otherwise just wait for completion
471 */
472 scsi_xsh_add(&sc->sc_xsh);
473
474 device_unref(&sc->sc_dev);
475 return;
476
477bad:
478 SET(bp->b_flags, B_ERROR)((bp->b_flags) |= (0x00000400));
479 bp->b_resid = bp->b_bcount;
480done:
481 s = splbio()splraise(0x3);
482 biodone(bp);
483 splx(s)spllower(s);
484 if (sc != NULL((void *)0))
485 device_unref(&sc->sc_dev);
486}
487
488int
489cd_cmd_rw6(struct scsi_generic *generic, int read, u_int64_t secno,
490 u_int32_t nsecs)
491{
492 struct scsi_rw *cmd = (struct scsi_rw *)generic;
493
494 cmd->opcode = read ? READ_COMMAND0x08 : WRITE_COMMAND0x0a;
495 _lto3b(secno, cmd->addr);
496 cmd->length = nsecs & 0xff;
497
498 return sizeof(*cmd);
499}
500
501int
502cd_cmd_rw10(struct scsi_generic *generic, int read, u_int64_t secno,
503 u_int32_t nsecs)
504{
505 struct scsi_rw_10 *cmd = (struct scsi_rw_10 *)generic;
506
507 cmd->opcode = read ? READ_100x28 : WRITE_100x2a;
508 _lto4b(secno, cmd->addr);
509 _lto2b(nsecs, cmd->length);
510
511 return sizeof(*cmd);
512}
513
514int
515cd_cmd_rw12(struct scsi_generic *generic, int read, u_int64_t secno,
516 u_int32_t nsecs)
517{
518 struct scsi_rw_12 *cmd = (struct scsi_rw_12 *)generic;
519
520 cmd->opcode = read ? READ_120xa8 : WRITE_120xaa;
521 _lto4b(secno, cmd->addr);
522 _lto4b(nsecs, cmd->length);
523
524 return sizeof(*cmd);
525}
526
527/*
528 * cdstart looks to see if there is a buf waiting for the device
529 * and that the device is not already busy. If both are true,
530 * It dequeues the buf and creates a scsi command to perform the
531 * transfer in the buf. The transfer request will call scsi_done
532 * on completion, which will in turn call this routine again
533 * so that the next queued transfer is performed.
534 * The bufs are queued by the strategy routine (cdstrategy)
535 *
536 * This routine is also called after other non-queued requests
537 * have been made of the scsi driver, to ensure that the queue
538 * continues to be drained.
539 *
540 * must be called at the correct (highish) spl level
541 * cdstart() is called at splbio from cdstrategy and scsi_done
542 */
543void
544cdstart(struct scsi_xfer *xs)
545{
546 struct scsi_link *link = xs->sc_link;
547 struct cd_softc *sc = link->device_softc;
548 struct buf *bp;
549 struct partition *p;
550 u_int64_t secno;
551 u_int32_t nsecs;
552 int read;
553
554 SC_DEBUG(link, SDEV_DB2, ("cdstart\n"));
555
556 if (ISSET(sc->sc_flags, CDF_DYING)((sc->sc_flags) & (0x40))) {
557 scsi_xs_put(xs);
558 return;
559 }
560
561 if (!ISSET(link->flags, SDEV_MEDIA_LOADED)((link->flags) & (0x0002))) {
562 bufq_drain(&sc->sc_bufq);
563 scsi_xs_put(xs);
564 return;
565 }
566
567 bp = bufq_dequeue(&sc->sc_bufq);
568 if (bp == NULL((void *)0)) {
569 scsi_xs_put(xs);
570 return;
571 }
572 read = ISSET(bp->b_flags, B_READ)((bp->b_flags) & (0x00008000));
573
574 SET(xs->flags, (read ? SCSI_DATA_IN : SCSI_DATA_OUT))((xs->flags) |= ((read ? 0x00800 : 0x01000)));
575 xs->timeout = 30000;
576 xs->data = bp->b_data;
577 xs->datalen = bp->b_bcount;
578 xs->done = cd_buf_done;
579 xs->cookie = bp;
580 xs->bp = bp;
581
582 p = &sc->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)(((unsigned)((bp->b_dev) & 0xff) | (((bp->b_dev) &
0xffff0000) >> 8)) % 16)
];
583 secno = DL_GETPOFFSET(p)(((u_int64_t)(p)->p_offseth << 32) + (p)->p_offset
)
+ DL_BLKTOSEC(sc->sc_dk.dk_label, bp->b_blkno)((bp->b_blkno) / ((sc->sc_dk.dk_label)->d_secsize / (
1 << 9)))
;
584 nsecs = howmany(bp->b_bcount, sc->sc_dk.dk_label->d_secsize)(((bp->b_bcount) + ((sc->sc_dk.dk_label->d_secsize) -
1)) / (sc->sc_dk.dk_label->d_secsize))
;
585
586 if (!ISSET(link->flags, SDEV_ATAPI | SDEV_UMASS)((link->flags) & (0x0200 | 0x0400)) &&
587 (SID_ANSII_REV(&link->inqdata)((&link->inqdata)->version & 0x07) < SCSI_REV_20x02) &&
588 ((secno & 0x1fffff) == secno) &&
589 ((nsecs & 0xff) == nsecs))
590 xs->cmdlen = cd_cmd_rw6(&xs->cmd, read, secno, nsecs);
591 else if (((secno & 0xffffffff) == secno) &&
592 ((nsecs & 0xffff) == nsecs))
593 xs->cmdlen = cd_cmd_rw10(&xs->cmd, read, secno, nsecs);
594 else
595 xs->cmdlen = cd_cmd_rw12(&xs->cmd, read, secno, nsecs);
596
597 disk_busy(&sc->sc_dk);
598 scsi_xs_exec(xs);
599
600 /* Move onto the next io. */
601 if (bufq_peek(&sc->sc_bufq))
602 scsi_xsh_add(&sc->sc_xsh);
603}
604
605void
606cd_buf_done(struct scsi_xfer *xs)
607{
608 struct cd_softc *sc = xs->sc_link->device_softc;
609 struct buf *bp = xs->cookie;
610 int error, s;
611
612 switch (xs->error) {
613 case XS_NOERROR0:
614 bp->b_error = 0;
615 CLR(bp->b_flags, B_ERROR)((bp->b_flags) &= ~(0x00000400));
616 bp->b_resid = xs->resid;
617 break;
618
619 case XS_SENSE1:
620 case XS_SHORTSENSE6:
621 SC_DEBUG_SENSE(xs);
622 error = cd_interpret_sense(xs);
623 if (error == 0) {
624 bp->b_error = 0;
625 CLR(bp->b_flags, B_ERROR)((bp->b_flags) &= ~(0x00000400));
626 bp->b_resid = xs->resid;
627 break;
628 }
629 if (error != ERESTART-1)
630 xs->retries = 0;
631 goto retry;
632
633 case XS_BUSY5:
634 if (xs->retries) {
635 if (scsi_delay(xs, 1) != ERESTART-1)
636 xs->retries = 0;
637 }
638 goto retry;
639
640 case XS_TIMEOUT4:
641retry:
642 if (xs->retries--) {
643 scsi_xs_exec(xs);
644 return;
645 }
646 /* FALLTHROUGH */
647
648 default:
649 bp->b_error = EIO5;
650 SET(bp->b_flags, B_ERROR)((bp->b_flags) |= (0x00000400));
651 bp->b_resid = bp->b_bcount;
652 break;
653 }
654
655 disk_unbusy(&sc->sc_dk, bp->b_bcount - xs->resid, bp->b_blkno,
656 bp->b_flags & B_READ0x00008000);
657
658 s = splbio()splraise(0x3);
659 biodone(bp);
660 splx(s)spllower(s);
661 scsi_xs_put(xs);
662}
663
664void
665cdminphys(struct buf *bp)
666{
667 struct scsi_link *link;
668 struct cd_softc *sc;
669 long max;
670
671 sc = cdlookup(DISKUNIT(bp->b_dev))(struct cd_softc *)disk_lookup(&cd_cd, ((((unsigned)((bp->
b_dev) & 0xff) | (((bp->b_dev) & 0xffff0000) >>
8)) / 16)))
;
672 if (sc == NULL((void *)0))
673 return;
674 link = sc->sc_link;
675
676 /*
677 * If the device is ancient, we want to make sure that
678 * the transfer fits into a 6-byte cdb.
679 *
680 * XXX Note that the SCSI-I spec says that 256-block transfers
681 * are allowed in a 6-byte read/write, and are specified
682 * by setting the "length" to 0. However, we're conservative
683 * here, allowing only 255-block transfers in case an
684 * ancient device gets confused by length == 0. A length of 0
685 * in a 10-byte read/write actually means 0 blocks.
686 */
687 if (!ISSET(link->flags, SDEV_ATAPI | SDEV_UMASS)((link->flags) & (0x0200 | 0x0400)) &&
688 SID_ANSII_REV(&link->inqdata)((&link->inqdata)->version & 0x07) < SCSI_REV_20x02) {
689 max = sc->sc_dk.dk_label->d_secsize * 0xff;
690
691 if (bp->b_bcount > max)
692 bp->b_bcount = max;
693 }
694
695 if (link->bus->sb_adapter->dev_minphys != NULL((void *)0))
696 (*link->bus->sb_adapter->dev_minphys)(bp, link);
697 else
698 minphys(bp);
699
700 device_unref(&sc->sc_dev);
701}
702
703int
704cdread(dev_t dev, struct uio *uio, int ioflag)
705{
706 return physio(cdstrategy, dev, B_READ0x00008000, cdminphys, uio);
707}
708
709int
710cdwrite(dev_t dev, struct uio *uio, int ioflag)
711{
712 return physio(cdstrategy, dev, B_WRITE0x00000000, cdminphys, uio);
713}
714
715/*
716 * Perform special action on behalf of the user.
717 * Knows about the internals of this device
718 */
719int
720cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
721{
722 struct cd_softc *sc;
723 struct disklabel *lp;
724 int part = DISKPART(dev)(((unsigned)((dev) & 0xff) | (((dev) & 0xffff0000) >>
8)) % 16)
;
725 int error = 0;
726
727 sc = cdlookup(DISKUNIT(dev))(struct cd_softc *)disk_lookup(&cd_cd, ((((unsigned)((dev
) & 0xff) | (((dev) & 0xffff0000) >> 8)) / 16))
)
;
728 if (sc == NULL((void *)0))
729 return ENXIO6;
730 if (ISSET(sc->sc_flags, CDF_DYING)((sc->sc_flags) & (0x40))) {
731 device_unref(&sc->sc_dev);
732 return ENXIO6;
733 }
734
735 SC_DEBUG(sc->sc_link, SDEV_DB2, ("cdioctl 0x%lx\n", cmd));
736
737 /*
738 * If the device is not valid.. abandon ship
739 */
740 if (!ISSET(sc->sc_link->flags, SDEV_MEDIA_LOADED)((sc->sc_link->flags) & (0x0002))) {
741 switch (cmd) {
742 case DIOCLOCK((unsigned long)0x80000000 | ((sizeof(int) & 0x1fff) <<
16) | ((('d')) << 8) | ((113)))
:
743 case DIOCEJECT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('d')) << 8) | ((112)))
:
744 case SCIOCIDENTIFY((unsigned long)0x40000000 | ((sizeof(struct scsi_addr) &
0x1fff) << 16) | ((('Q')) << 8) | ((9)))
:
745 case SCIOCCOMMAND(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(scsireq_t) & 0x1fff) << 16) | ((('Q')) << 8)
| ((1)))
:
746 case SCIOCDEBUG((unsigned long)0x80000000 | ((sizeof(int) & 0x1fff) <<
16) | ((('Q')) << 8) | ((2)))
:
747 case CDIOCLOADUNLOAD((unsigned long)0x80000000 | ((sizeof(struct ioc_load_unload)
& 0x1fff) << 16) | ((('c')) << 8) | ((26)))
:
748 case SCIOCRESET((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('Q')) << 8) | ((7)))
:
749 case CDIOCGETVOL((unsigned long)0x40000000 | ((sizeof(struct ioc_vol) & 0x1fff
) << 16) | ((('c')) << 8) | ((10)))
:
750 case CDIOCSETVOL((unsigned long)0x80000000 | ((sizeof(struct ioc_vol) & 0x1fff
) << 16) | ((('c')) << 8) | ((11)))
:
751 case CDIOCSETMONO((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((12)))
:
752 case CDIOCSETSTEREO((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((13)))
:
753 case CDIOCSETMUTE((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((14)))
:
754 case CDIOCSETLEFT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((15)))
:
755 case CDIOCSETRIGHT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((16)))
:
756 case CDIOCCLOSE((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((27)))
:
757 case CDIOCEJECT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((24)))
:
758 case CDIOCALLOW((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((25)))
:
759 case CDIOCPREVENT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((26)))
:
760 case CDIOCSETDEBUG((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((17)))
:
761 case CDIOCCLRDEBUG((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((18)))
:
762 case CDIOCRESET((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((21)))
:
763 case DVD_AUTH(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(union dvd_authinfo) & 0x1fff) << 16) | ((('d')) <<
8) | ((2)))
:
764 case DVD_READ_STRUCT(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(union dvd_struct) & 0x1fff) << 16) | ((('d')) <<
8) | ((0)))
:
765 case MTIOCTOP((unsigned long)0x80000000 | ((sizeof(struct mtop) & 0x1fff
) << 16) | ((('m')) << 8) | ((1)))
:
766 if (part == RAW_PART2)
767 break;
768 /* FALLTHROUGH */
769 default:
770 if (!ISSET(sc->sc_link->flags, SDEV_OPEN)((sc->sc_link->flags) & (0x0008)))
771 error = ENODEV19;
772 else
773 error = EIO5;
774 goto exit;
775 }
776 }
777
778 switch (cmd) {
779 case DIOCRLDINFO((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('d')) << 8) | ((115)))
:
780 lp = malloc(sizeof(*lp), M_TEMP127, M_WAITOK0x0001);
781 cdgetdisklabel(dev, sc, lp, 0);
782 memcpy(sc->sc_dk.dk_label, lp, sizeof(*lp))__builtin_memcpy((sc->sc_dk.dk_label), (lp), (sizeof(*lp))
)
;
783 free(lp, M_TEMP127, sizeof(*lp));
784 break;
785
786 case DIOCGPDINFO((unsigned long)0x40000000 | ((sizeof(struct disklabel) &
0x1fff) << 16) | ((('d')) << 8) | ((114)))
:
787 cdgetdisklabel(dev, sc, (struct disklabel *)addr, 1);
788 break;
789
790 case DIOCGDINFO((unsigned long)0x40000000 | ((sizeof(struct disklabel) &
0x1fff) << 16) | ((('d')) << 8) | ((101)))
:
791 *(struct disklabel *)addr = *(sc->sc_dk.dk_label);
792 break;
793
794 case DIOCGPART((unsigned long)0x80000000 | ((sizeof(struct partinfo) & 0x1fff
) << 16) | ((('d')) << 8) | ((104)))
:
795 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
796 ((struct partinfo *)addr)->part =
797 &sc->sc_dk.dk_label->d_partitions[DISKPART(dev)(((unsigned)((dev) & 0xff) | (((dev) & 0xffff0000) >>
8)) % 16)
];
798 break;
799
800 case DIOCWDINFO((unsigned long)0x80000000 | ((sizeof(struct disklabel) &
0x1fff) << 16) | ((('d')) << 8) | ((103)))
:
801 case DIOCSDINFO((unsigned long)0x80000000 | ((sizeof(struct disklabel) &
0x1fff) << 16) | ((('d')) << 8) | ((102)))
:
802 if (!ISSET(flag, FWRITE)((flag) & (0x0002))) {
803 error = EBADF9;
804 break;
805 }
806
807 if ((error = disk_lock(&sc->sc_dk)) != 0)
808 break;
809
810 error = setdisklabel(sc->sc_dk.dk_label,
811 (struct disklabel *)addr, sc->sc_dk.dk_openmask);
812 if (error == 0) {
813 }
814
815 disk_unlock(&sc->sc_dk);
816 break;
817
818 case CDIOCPLAYTRACKS((unsigned long)0x80000000 | ((sizeof(struct ioc_play_track) &
0x1fff) << 16) | ((('c')) << 8) | ((1)))
: {
819 struct ioc_play_track *args = (struct ioc_play_track *)addr;
820
821 if ((error = cd_set_pa_immed(sc, 0)) != 0)
822 break;
823 error = cd_play_tracks(sc, args->start_track,
824 args->start_index, args->end_track, args->end_index);
825 break;
826 }
827 case CDIOCPLAYMSF((unsigned long)0x80000000 | ((sizeof(struct ioc_play_msf) &
0x1fff) << 16) | ((('c')) << 8) | ((25)))
: {
828 struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
829
830 if ((error = cd_set_pa_immed(sc, 0)) != 0)
831 break;
832 error = cd_play_msf(sc, args->start_m, args->start_s,
833 args->start_f, args->end_m, args->end_s, args->end_f);
834 break;
835 }
836 case CDIOCPLAYBLOCKS((unsigned long)0x80000000 | ((sizeof(struct ioc_play_blocks)
& 0x1fff) << 16) | ((('c')) << 8) | ((2)))
: {
837 struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
838
839 if ((error = cd_set_pa_immed(sc, 0)) != 0)
840 break;
841 error = cd_play(sc, args->blk, args->len);
842 break;
843 }
844 case CDIOCREADSUBCHANNEL(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(struct ioc_read_subchannel) & 0x1fff) << 16) | (((
'c')) << 8) | ((3)))
: {
845 struct ioc_read_subchannel *args =
846 (struct ioc_read_subchannel *)addr;
847 struct cd_sub_channel_info *data;
848 int len = args->data_len;
849
850 if (len > sizeof(*data) ||
851 len < sizeof(struct cd_sub_channel_header)) {
852 error = EINVAL22;
853 break;
854 }
855 data = dma_alloc(sizeof(*data), PR_WAITOK0x0001);
856 error = cd_read_subchannel(sc, args->address_format,
857 args->data_format, args->track, data, len);
858 if (error) {
859 dma_free(data, sizeof(*data));
860 break;
861 }
862 len = min(len, _2btol(data->header.data_len) +
863 sizeof(struct cd_sub_channel_header));
864 error = copyout(data, args->data, len);
865 dma_free(data, sizeof(*data));
866 break;
867 }
868 case CDIOREADTOCHEADER((unsigned long)0x40000000 | ((sizeof(struct ioc_toc_header) &
0x1fff) << 16) | ((('c')) << 8) | ((4)))
: {
869 struct ioc_toc_header *th;
870
871 th = dma_alloc(sizeof(*th), PR_WAITOK0x0001);
872 if ((error = cd_read_toc(sc, 0, 0, th, sizeof(*th), 0)) != 0) {
873 dma_free(th, sizeof(*th));
874 break;
875 }
876 if (ISSET(sc->sc_link->quirks, ADEV_LITTLETOC)((sc->sc_link->quirks) & (0x0040)))
877 th->len = letoh16(th->len)((__uint16_t)(th->len));
878 else
879 th->len = betoh16(th->len)(__uint16_t)(__builtin_constant_p(th->len) ? (__uint16_t)(
((__uint16_t)(th->len) & 0xffU) << 8 | ((__uint16_t
)(th->len) & 0xff00U) >> 8) : __swap16md(th->
len))
;
880 if (th->len > 0)
881 memcpy(addr, th, sizeof(*th))__builtin_memcpy((addr), (th), (sizeof(*th)));
882 else
883 error = EIO5;
884 dma_free(th, sizeof(*th));
885 break;
886 }
887 case CDIOREADTOCENTRYS(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(struct ioc_read_toc_entry) & 0x1fff) << 16) | ((('c'
)) << 8) | ((5)))
: {
888 struct cd_toc *toc;
889 struct ioc_read_toc_entry *te =
890 (struct ioc_read_toc_entry *)addr;
891 struct ioc_toc_header *th;
892 struct cd_toc_entry *cte;
893 int len = te->data_len;
894 int ntracks;
895
896 toc = dma_alloc(sizeof(*toc), PR_WAITOK0x0001 | PR_ZERO0x0008);
897
898 th = &toc->header;
899
900 if (len > sizeof(toc->entries) ||
901 len < sizeof(struct cd_toc_entry)) {
902 dma_free(toc, sizeof(*toc));
903 error = EINVAL22;
904 break;
905 }
906 error = cd_read_toc(sc, te->address_format, te->starting_track,
907 toc, len + sizeof(struct ioc_toc_header), 0);
908 if (error) {
909 dma_free(toc, sizeof(*toc));
910 break;
911 }
912 if (te->address_format == CD_LBA_FORMAT1)
913 for (ntracks =
914 th->ending_track - th->starting_track + 1;
915 ntracks >= 0; ntracks--) {
916 cte = &toc->entries[ntracks];
917 cte->addr_type = CD_LBA_FORMAT1;
918 if (ISSET(sc->sc_link->quirks,((sc->sc_link->quirks) & (0x0040))
919 ADEV_LITTLETOC)((sc->sc_link->quirks) & (0x0040))) {
920#if BYTE_ORDER1234 == BIG_ENDIAN4321
921 swap16_multi((u_int16_t *)&cte->addr,do { __size_t __swap16_multi_n = (sizeof(cte->addr) / 2); __uint16_t
*__swap16_multi_v = ((u_int16_t *)&cte->addr); while (
__swap16_multi_n) { *__swap16_multi_v = (__uint16_t)(__builtin_constant_p
(*__swap16_multi_v) ? (__uint16_t)(((__uint16_t)(*__swap16_multi_v
) & 0xffU) << 8 | ((__uint16_t)(*__swap16_multi_v) &
0xff00U) >> 8) : __swap16md(*__swap16_multi_v)); __swap16_multi_v
++; __swap16_multi_n--; } } while (0)
922 sizeof(cte->addr) / 2)do { __size_t __swap16_multi_n = (sizeof(cte->addr) / 2); __uint16_t
*__swap16_multi_v = ((u_int16_t *)&cte->addr); while (
__swap16_multi_n) { *__swap16_multi_v = (__uint16_t)(__builtin_constant_p
(*__swap16_multi_v) ? (__uint16_t)(((__uint16_t)(*__swap16_multi_v
) & 0xffU) << 8 | ((__uint16_t)(*__swap16_multi_v) &
0xff00U) >> 8) : __swap16md(*__swap16_multi_v)); __swap16_multi_v
++; __swap16_multi_n--; } } while (0)
;
923#endif /* BYTE_ORDER == BIG_ENDIAN */
924 } else
925 cte->addr.lba = betoh32(cte->addr.lba)(__uint32_t)(__builtin_constant_p(cte->addr.lba) ? (__uint32_t
)(((__uint32_t)(cte->addr.lba) & 0xff) << 24 | (
(__uint32_t)(cte->addr.lba) & 0xff00) << 8 | ((__uint32_t
)(cte->addr.lba) & 0xff0000) >> 8 | ((__uint32_t
)(cte->addr.lba) & 0xff000000) >> 24) : __swap32md
(cte->addr.lba))
;
926 }
927 if (ISSET(sc->sc_link->quirks, ADEV_LITTLETOC)((sc->sc_link->quirks) & (0x0040))) {
928 th->len = letoh16(th->len)((__uint16_t)(th->len));
929 } else
930 th->len = betoh16(th->len)(__uint16_t)(__builtin_constant_p(th->len) ? (__uint16_t)(
((__uint16_t)(th->len) & 0xffU) << 8 | ((__uint16_t
)(th->len) & 0xff00U) >> 8) : __swap16md(th->
len))
;
931 len = min(len, th->len - (sizeof(th->starting_track) +
932 sizeof(th->ending_track)));
933
934 error = copyout(toc->entries, te->data, len);
935 dma_free(toc, sizeof(*toc));
936 break;
937 }
938 case CDIOREADMSADDR(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(int) & 0x1fff) << 16) | ((('c')) << 8) | ((6
)))
: {
939 struct cd_toc *toc;
940 int sessno = *(int *)addr;
941 struct cd_toc_entry *cte;
942
943 if (sessno != 0) {
944 error = EINVAL22;
945 break;
946 }
947
948 toc = dma_alloc(sizeof(*toc), PR_WAITOK0x0001 | PR_ZERO0x0008);
949
950 error = cd_read_toc(sc, 0, 0, toc,
951 sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry),
952 0x40 /* control word for "get MS info" */);
953
954 if (error) {
955 dma_free(toc, sizeof(*toc));
956 break;
957 }
958
959 cte = &toc->entries[0];
960 if (ISSET(sc->sc_link->quirks, ADEV_LITTLETOC)((sc->sc_link->quirks) & (0x0040))) {
961#if BYTE_ORDER1234 == BIG_ENDIAN4321
962 swap16_multi((u_int16_t *)&cte->addr,do { __size_t __swap16_multi_n = (sizeof(cte->addr) / 2); __uint16_t
*__swap16_multi_v = ((u_int16_t *)&cte->addr); while (
__swap16_multi_n) { *__swap16_multi_v = (__uint16_t)(__builtin_constant_p
(*__swap16_multi_v) ? (__uint16_t)(((__uint16_t)(*__swap16_multi_v
) & 0xffU) << 8 | ((__uint16_t)(*__swap16_multi_v) &
0xff00U) >> 8) : __swap16md(*__swap16_multi_v)); __swap16_multi_v
++; __swap16_multi_n--; } } while (0)
963 sizeof(cte->addr) / 2)do { __size_t __swap16_multi_n = (sizeof(cte->addr) / 2); __uint16_t
*__swap16_multi_v = ((u_int16_t *)&cte->addr); while (
__swap16_multi_n) { *__swap16_multi_v = (__uint16_t)(__builtin_constant_p
(*__swap16_multi_v) ? (__uint16_t)(((__uint16_t)(*__swap16_multi_v
) & 0xffU) << 8 | ((__uint16_t)(*__swap16_multi_v) &
0xff00U) >> 8) : __swap16md(*__swap16_multi_v)); __swap16_multi_v
++; __swap16_multi_n--; } } while (0)
;
964#endif /* BYTE_ORDER == BIG_ENDIAN */
965 } else
966 cte->addr.lba = betoh32(cte->addr.lba)(__uint32_t)(__builtin_constant_p(cte->addr.lba) ? (__uint32_t
)(((__uint32_t)(cte->addr.lba) & 0xff) << 24 | (
(__uint32_t)(cte->addr.lba) & 0xff00) << 8 | ((__uint32_t
)(cte->addr.lba) & 0xff0000) >> 8 | ((__uint32_t
)(cte->addr.lba) & 0xff000000) >> 24) : __swap32md
(cte->addr.lba))
;
967 if (ISSET(sc->sc_link->quirks, ADEV_LITTLETOC)((sc->sc_link->quirks) & (0x0040)))
968 toc->header.len = letoh16(toc->header.len)((__uint16_t)(toc->header.len));
969 else
970 toc->header.len = betoh16(toc->header.len)(__uint16_t)(__builtin_constant_p(toc->header.len) ? (__uint16_t
)(((__uint16_t)(toc->header.len) & 0xffU) << 8 |
((__uint16_t)(toc->header.len) & 0xff00U) >> 8)
: __swap16md(toc->header.len))
;
971
972 *(int *)addr = (toc->header.len >= 10 && cte->track > 1) ?
973 cte->addr.lba : 0;
974 dma_free(toc, sizeof(*toc));
975 break;
976 }
977 case CDIOCSETPATCH((unsigned long)0x80000000 | ((sizeof(struct ioc_patch) &
0x1fff) << 16) | ((('c')) << 8) | ((9)))
: {
978 struct ioc_patch *arg = (struct ioc_patch *)addr;
979
980 error = cd_setchan(sc, arg->patch[0], arg->patch[1],
981 arg->patch[2], arg->patch[3], 0);
982 break;
983 }
984 case CDIOCGETVOL((unsigned long)0x40000000 | ((sizeof(struct ioc_vol) & 0x1fff
) << 16) | ((('c')) << 8) | ((10)))
: {
985 struct ioc_vol *arg = (struct ioc_vol *)addr;
986
987 error = cd_getvol(sc, arg, 0);
988 break;
989 }
990 case CDIOCSETVOL((unsigned long)0x80000000 | ((sizeof(struct ioc_vol) & 0x1fff
) << 16) | ((('c')) << 8) | ((11)))
: {
991 struct ioc_vol *arg = (struct ioc_vol *)addr;
992
993 error = cd_setvol(sc, arg, 0);
994 break;
995 }
996
997 case CDIOCSETMONO((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((12)))
:
998 error = cd_setchan(sc, BOTH_CHANNEL1 | 2, BOTH_CHANNEL1 | 2, MUTE_CHANNEL0x0,
999 MUTE_CHANNEL0x0, 0);
1000 break;
1001
1002 case CDIOCSETSTEREO((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((13)))
:
1003 error = cd_setchan(sc, LEFT_CHANNEL1, RIGHT_CHANNEL2,
1004 MUTE_CHANNEL0x0, MUTE_CHANNEL0x0, 0);
1005 break;
1006
1007 case CDIOCSETMUTE((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((14)))
:
1008 error = cd_setchan(sc, MUTE_CHANNEL0x0, MUTE_CHANNEL0x0, MUTE_CHANNEL0x0,
1009 MUTE_CHANNEL0x0, 0);
1010 break;
1011
1012 case CDIOCSETLEFT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((15)))
:
1013 error = cd_setchan(sc, LEFT_CHANNEL1, LEFT_CHANNEL1, MUTE_CHANNEL0x0,
1014 MUTE_CHANNEL0x0, 0);
1015 break;
1016
1017 case CDIOCSETRIGHT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((16)))
:
1018 error = cd_setchan(sc, RIGHT_CHANNEL2, RIGHT_CHANNEL2,
1019 MUTE_CHANNEL0x0, MUTE_CHANNEL0x0, 0);
1020 break;
1021
1022 case CDIOCRESUME((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((20)))
:
1023 error = cd_pause(sc, 1);
1024 break;
1025
1026 case CDIOCPAUSE((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((19)))
:
1027 error = cd_pause(sc, 0);
1028 break;
1029 case CDIOCSTART((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((22)))
:
1030 error = scsi_start(sc->sc_link, SSS_START0x01, 0);
1031 break;
1032
1033 case CDIOCSTOP((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((23)))
:
1034 error = scsi_start(sc->sc_link, SSS_STOP0x00, 0);
1035 break;
1036
1037close_tray:
1038 case CDIOCCLOSE((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((27)))
:
1039 error = scsi_start(sc->sc_link, SSS_START0x01|SSS_LOEJ0x02,
1040 SCSI_IGNORE_NOT_READY0x00040 | SCSI_IGNORE_MEDIA_CHANGE0x00080);
1041 break;
1042
1043 case MTIOCTOP((unsigned long)0x80000000 | ((sizeof(struct mtop) & 0x1fff
) << 16) | ((('m')) << 8) | ((1)))
:
1044 if (((struct mtop *)addr)->mt_op == MTRETEN8)
1045 goto close_tray;
1046 if (((struct mtop *)addr)->mt_op != MTOFFL6) {
1047 error = EIO5;
1048 break;
1049 }
1050 /* FALLTHROUGH */
1051 case CDIOCEJECT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((24)))
: /* FALLTHROUGH */
1052 case DIOCEJECT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('d')) << 8) | ((112)))
:
1053 SET(sc->sc_link->flags, SDEV_EJECTING)((sc->sc_link->flags) |= (0x0100));
1054 break;
1055 case CDIOCALLOW((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((25)))
:
1056 error = scsi_prevent(sc->sc_link, PR_ALLOW0x00, 0);
1057 break;
1058 case CDIOCPREVENT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((26)))
:
1059 error = scsi_prevent(sc->sc_link, PR_PREVENT0x01, 0);
1060 break;
1061 case DIOCLOCK((unsigned long)0x80000000 | ((sizeof(int) & 0x1fff) <<
16) | ((('d')) << 8) | ((113)))
:
1062 error = scsi_prevent(sc->sc_link,
1063 (*(int *)addr) ? PR_PREVENT0x01 : PR_ALLOW0x00, 0);
1064 break;
1065 case CDIOCSETDEBUG((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((17)))
:
1066 SET(sc->sc_link->flags, SDEV_DB1 | SDEV_DB2)((sc->sc_link->flags) |= (0x0010 | 0x0020));
1067 break;
1068 case CDIOCCLRDEBUG((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((18)))
:
1069 CLR(sc->sc_link->flags, SDEV_DB1 | SDEV_DB2)((sc->sc_link->flags) &= ~(0x0010 | 0x0020));
1070 break;
1071 case CDIOCRESET((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((21)))
:
1072 case SCIOCRESET((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('Q')) << 8) | ((7)))
:
1073 error = cd_reset(sc);
1074 break;
1075 case CDIOCLOADUNLOAD((unsigned long)0x80000000 | ((sizeof(struct ioc_load_unload)
& 0x1fff) << 16) | ((('c')) << 8) | ((26)))
: {
1076 struct ioc_load_unload *args = (struct ioc_load_unload *)addr;
1077
1078 error = cd_load_unload(sc, args->options, args->slot);
1079 break;
1080 }
1081
1082 case DVD_AUTH(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(union dvd_authinfo) & 0x1fff) << 16) | ((('d')) <<
8) | ((2)))
:
1083 error = dvd_auth(sc, (union dvd_authinfo *)addr);
1084 break;
1085 case DVD_READ_STRUCT(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(union dvd_struct) & 0x1fff) << 16) | ((('d')) <<
8) | ((0)))
:
1086 error = dvd_read_struct(sc, (union dvd_struct *)addr);
1087 break;
1088 default:
1089 if (DISKPART(dev)(((unsigned)((dev) & 0xff) | (((dev) & 0xffff0000) >>
8)) % 16)
!= RAW_PART2) {
1090 error = ENOTTY25;
1091 break;
1092 }
1093 error = scsi_do_ioctl(sc->sc_link, cmd, addr, flag);
1094 break;
1095 }
1096
1097exit:
1098
1099 device_unref(&sc->sc_dev);
1100 return error;
1101}
1102
1103/*
1104 * Load the label information on the named device
1105 * Actually fabricate a disklabel
1106 *
1107 * EVENTUALLY take information about different
1108 * data tracks from the TOC and put it in the disklabel
1109 */
1110int
1111cdgetdisklabel(dev_t dev, struct cd_softc *sc, struct disklabel *lp,
1112 int spoofonly)
1113{
1114 struct cd_toc *toc;
1115 int tocidx, n, audioonly = 1;
1116
1117 bzero(lp, sizeof(struct disklabel))__builtin_bzero((lp), (sizeof(struct disklabel)));
1118
1119 lp->d_secsize = sc->params.secsize;
1120 lp->d_ntracks = 1;
1121 lp->d_nsectors = 100;
1122 lp->d_secpercyl = 100;
1123 lp->d_ncylinders = (sc->params.disksize / 100) + 1;
1124
1125 if (ISSET(sc->sc_link->flags, SDEV_ATAPI)((sc->sc_link->flags) & (0x0200))) {
1126 strncpy(lp->d_typename, "ATAPI CD-ROM", sizeof(lp->d_typename));
1127 lp->d_type = DTYPE_ATAPI13;
1128 } else {
1129 strncpy(lp->d_typename, "SCSI CD-ROM", sizeof(lp->d_typename));
1130 lp->d_type = DTYPE_SCSI4;
1131 }
1132
1133 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1134 DL_SETDSIZE(lp, sc->params.disksize)do { u_int64_t __x = (sc->params.disksize); (lp)->d_secperunith
= __x >> 32; (lp)->d_secperunit = __x; } while (0)
;
1135 lp->d_version = 1;
1136
1137 lp->d_magic = DISKMAGIC((u_int32_t)0x82564557);
1138 lp->d_magic2 = DISKMAGIC((u_int32_t)0x82564557);
1139 lp->d_checksum = dkcksum(lp);
1140
1141 toc = dma_alloc(sizeof(*toc), PR_WAITOK0x0001 | PR_ZERO0x0008);
1142 if (cd_load_toc(sc, toc, CD_LBA_FORMAT1)) {
1143 audioonly = 0; /* No valid TOC found == not an audio CD. */
1144 goto done;
1145 }
1146
1147 n = toc->header.ending_track - toc->header.starting_track + 1;
1148 for (tocidx = 0; tocidx < n; tocidx++)
1149 if (toc->entries[tocidx].control & 4) {
1150 audioonly = 0; /* Found a non-audio track. */
1151 goto done;
1152 }
1153
1154done:
1155 dma_free(toc, sizeof(*toc));
1156
1157 if (audioonly)
1158 return 0;
1159 return readdisklabel(DISKLABELDEV(dev)((((dev_t)((((((((unsigned)(dev) >> 8) & 0xff))) &
0xff) << 8) | (((((((((unsigned)((dev) & 0xff) | (
((dev) & 0xffff0000) >> 8)) / 16))) * 16) + ((2))))
& 0xff) | ((((((((((unsigned)((dev) & 0xff) | (((dev
) & 0xffff0000) >> 8)) / 16))) * 16) + ((2)))) &
0xffff00) << 8)))))
, cdstrategy, lp, spoofonly);
1160}
1161
1162int
1163cd_setchan(struct cd_softc *sc, int p0, int p1, int p2, int p3, int flags)
1164{
1165 union scsi_mode_sense_buf *data;
1166 struct cd_audio_page *audio = NULL((void *)0);
1167 int error, big;
1168
1169 data = dma_alloc(sizeof(*data), PR_NOWAIT0x0002);
1170 if (data == NULL((void *)0))
1171 return ENOMEM12;
1172
1173 error = scsi_do_mode_sense(sc->sc_link, AUDIO_PAGE0x0e, data,
1174 (void **)&audio, sizeof(*audio), flags, &big);
1175 if (error == 0 && audio == NULL((void *)0))
1176 error = EIO5;
1177
1178 if (error == 0) {
1179 audio->port[LEFT_PORT0].channels = p0;
1180 audio->port[RIGHT_PORT1].channels = p1;
1181 audio->port[2].channels = p2;
1182 audio->port[3].channels = p3;
1183 if (big)
1184 error = scsi_mode_select_big(sc->sc_link, SMS_PF0x10,
1185 &data->hdr_big, flags, 20000);
1186 else
1187 error = scsi_mode_select(sc->sc_link, SMS_PF0x10,
1188 &data->hdr, flags, 20000);
1189 }
1190
1191 dma_free(data, sizeof(*data));
1192 return error;
1193}
1194
1195int
1196cd_getvol(struct cd_softc *sc, struct ioc_vol *arg, int flags)
1197{
1198 union scsi_mode_sense_buf *data;
1199 struct cd_audio_page *audio = NULL((void *)0);
1200 int big, error;
1201
1202 data = dma_alloc(sizeof(*data), PR_NOWAIT0x0002);
1203 if (data == NULL((void *)0))
1204 return ENOMEM12;
1205
1206 error = scsi_do_mode_sense(sc->sc_link, AUDIO_PAGE0x0e, data,
1207 (void **)&audio, sizeof(*audio), flags, &big);
1208 if (error == 0 && audio == NULL((void *)0))
1209 error = EIO5;
1210
1211 if (error == 0) {
1212 arg->vol[0] = audio->port[0].volume;
1213 arg->vol[1] = audio->port[1].volume;
1214 arg->vol[2] = audio->port[2].volume;
1215 arg->vol[3] = audio->port[3].volume;
1216 }
1217
1218 dma_free(data, sizeof(*data));
1219 return 0;
1220}
1221
1222int
1223cd_setvol(struct cd_softc *sc, const struct ioc_vol *arg, int flags)
1224{
1225 union scsi_mode_sense_buf *data;
1226 struct cd_audio_page *audio = NULL((void *)0);
1227 u_int8_t mask_volume[4];
1228 int error, big;
1229
1230 data = dma_alloc(sizeof(*data), PR_NOWAIT0x0002);
1231 if (data == NULL((void *)0))
1232 return ENOMEM12;
1233
1234 error = scsi_do_mode_sense(sc->sc_link,
1235 AUDIO_PAGE0x0e | SMS_PAGE_CTRL_CHANGEABLE0x40, data, (void **)&audio,
1236 sizeof(*audio), flags, &big);
1237 if (error == 0 && audio == NULL((void *)0))
1238 error = EIO5;
1239 if (error != 0) {
1240 dma_free(data, sizeof(*data));
1241 return error;
1242 }
1243
1244 mask_volume[0] = audio->port[0].volume;
1245 mask_volume[1] = audio->port[1].volume;
1246 mask_volume[2] = audio->port[2].volume;
1247 mask_volume[3] = audio->port[3].volume;
1248
1249 error = scsi_do_mode_sense(sc->sc_link, AUDIO_PAGE0x0e, data,
1250 (void **)&audio, sizeof(*audio), flags, &big);
1251 if (error == 0 && audio == NULL((void *)0))
1252 error = EIO5;
1253 if (error != 0) {
1254 dma_free(data, sizeof(*data));
1255 return error;
1256 }
1257
1258 audio->port[0].volume = arg->vol[0] & mask_volume[0];
1259 audio->port[1].volume = arg->vol[1] & mask_volume[1];
1260 audio->port[2].volume = arg->vol[2] & mask_volume[2];
1261 audio->port[3].volume = arg->vol[3] & mask_volume[3];
1262
1263 if (big)
1264 error = scsi_mode_select_big(sc->sc_link, SMS_PF0x10,
1265 &data->hdr_big, flags, 20000);
1266 else
1267 error = scsi_mode_select(sc->sc_link, SMS_PF0x10,
1268 &data->hdr, flags, 20000);
1269
1270 dma_free(data, sizeof(*data));
1271 return error;
1272}
1273
1274int
1275cd_load_unload(struct cd_softc *sc, int options, int slot)
1276{
1277 struct scsi_load_unload *cmd;
1278 struct scsi_xfer *xs;
1279 int error;
1280
1281 xs = scsi_xs_get(sc->sc_link, 0);
1282 if (xs == NULL((void *)0))
1283 return ENOMEM12;
1284 xs->cmdlen = sizeof(*cmd);
1285 xs->timeout = 200000;
1286
1287 cmd = (struct scsi_load_unload *)&xs->cmd;
1288 cmd->opcode = LOAD_UNLOAD0xa6;
1289 cmd->options = options; /* ioctl uses ATAPI values */
1290 cmd->slot = slot;
1291
1292 error = scsi_xs_sync(xs);
1293 scsi_xs_put(xs);
1294
1295 return error;
1296}
1297
1298int
1299cd_set_pa_immed(struct cd_softc *sc, int flags)
1300{
1301 union scsi_mode_sense_buf *data;
1302 struct cd_audio_page *audio = NULL((void *)0);
1303 int error, oflags, big;
1304
1305 if (ISSET(sc->sc_link->flags, SDEV_ATAPI)((sc->sc_link->flags) & (0x0200)))
1306 /* XXX Noop? */
1307 return 0;
1308
1309 data = dma_alloc(sizeof(*data), PR_NOWAIT0x0002);
1310 if (data == NULL((void *)0))
1311 return ENOMEM12;
1312
1313 error = scsi_do_mode_sense(sc->sc_link, AUDIO_PAGE0x0e, data,
1314 (void **)&audio, sizeof(*audio), flags, &big);
1315 if (error == 0 && audio == NULL((void *)0))
1316 error = EIO5;
1317
1318 if (error == 0) {
1319 oflags = audio->flags;
1320 CLR(audio->flags, CD_PA_SOTC)((audio->flags) &= ~(0x02));
1321 SET(audio->flags, CD_PA_IMMED)((audio->flags) |= (0x04));
1322 if (audio->flags != oflags) {
1323 if (big)
1324 error = scsi_mode_select_big(sc->sc_link,
1325 SMS_PF0x10, &data->hdr_big, flags, 20000);
1326 else
1327 error = scsi_mode_select(sc->sc_link, SMS_PF0x10,
1328 &data->hdr, flags, 20000);
1329 }
1330 }
1331
1332 dma_free(data, sizeof(*data));
1333 return error;
1334}
1335
1336/*
1337 * Get scsi driver to send a "start playing" command
1338 */
1339int
1340cd_play(struct cd_softc *sc, int secno, int nsecs)
1341{
1342 struct scsi_play *cmd;
1343 struct scsi_xfer *xs;
1344 int error;
1345
1346 xs = scsi_xs_get(sc->sc_link, 0);
1347 if (xs == NULL((void *)0))
1348 return ENOMEM12;
1349 xs->cmdlen = sizeof(*cmd);
1350 xs->timeout = 200000;
1351
1352 cmd = (struct scsi_play *)&xs->cmd;
1353 cmd->opcode = PLAY0x45;
1354 _lto4b(secno, cmd->blk_addr);
1355 _lto2b(nsecs, cmd->xfer_len);
1356
1357 error = scsi_xs_sync(xs);
1358 scsi_xs_put(xs);
1359
1360 return error;
1361}
1362
1363/*
1364 * Get scsi driver to send a "start playing" command
1365 */
1366int
1367cd_play_tracks(struct cd_softc *sc, int strack, int sindex, int etrack,
1368 int eindex)
1369{
1370 struct cd_toc *toc;
1371 int error;
1372 u_char endf, ends, endm;
1373
1374 if (!etrack)
1375 return EIO5;
1376 if (strack > etrack)
1377 return EINVAL22;
1378
1379 toc = dma_alloc(sizeof(*toc), PR_WAITOK0x0001 | PR_ZERO0x0008);
1380
1381 if ((error = cd_load_toc(sc, toc, CD_MSF_FORMAT2)) != 0)
1382 goto done;
1383
1384 if (++etrack > (toc->header.ending_track+1))
1385 etrack = toc->header.ending_track+1;
1386
1387 strack -= toc->header.starting_track;
1388 etrack -= toc->header.starting_track;
1389 if (strack < 0) {
1390 error = EINVAL22;
1391 goto done;
1392 }
1393
1394 /*
1395 * The track ends one frame before the next begins. The last track
1396 * is taken care of by the leadoff track.
1397 */
1398 endm = toc->entries[etrack].addr.msf.minute;
1399 ends = toc->entries[etrack].addr.msf.second;
1400 endf = toc->entries[etrack].addr.msf.frame;
1401 if (endf-- == 0) {
1402 endf = CD_FRAMES75 - 1;
1403 if (ends-- == 0) {
1404 ends = CD_SECS60 - 1;
1405 if (endm-- == 0) {
1406 error = EINVAL22;
1407 goto done;
1408 }
1409 }
1410 }
1411
1412 error = cd_play_msf(sc, toc->entries[strack].addr.msf.minute,
1413 toc->entries[strack].addr.msf.second,
1414 toc->entries[strack].addr.msf.frame,
1415 endm, ends, endf);
1416
1417done:
1418 dma_free(toc, sizeof(*toc));
1419 return error;
1420}
1421
1422/*
1423 * Get scsi driver to send a "play msf" command
1424 */
1425int
1426cd_play_msf(struct cd_softc *sc, int startm, int starts, int startf, int endm,
1427 int ends, int endf)
1428{
1429 struct scsi_play_msf *cmd;
1430 struct scsi_xfer *xs;
1431 int error;
1432
1433 xs = scsi_xs_get(sc->sc_link, 0);
1434 if (xs == NULL((void *)0))
1435 return ENOMEM12;
1436 xs->cmdlen = sizeof(*cmd);
1437 xs->timeout = 20000;
1438
1439 cmd = (struct scsi_play_msf *)&xs->cmd;
1440 cmd->opcode = PLAY_MSF0x47;
1441 cmd->start_m = startm;
1442 cmd->start_s = starts;
1443 cmd->start_f = startf;
1444 cmd->end_m = endm;
1445 cmd->end_s = ends;
1446 cmd->end_f = endf;
1447
1448 error = scsi_xs_sync(xs);
1449 scsi_xs_put(xs);
1450
1451 return error;
1452}
1453
1454/*
1455 * Get scsi driver to send a "start up" command
1456 */
1457int
1458cd_pause(struct cd_softc *sc, int go)
1459{
1460 struct scsi_pause *cmd;
1461 struct scsi_xfer *xs;
1462 int error;
1463
1464 xs = scsi_xs_get(sc->sc_link, 0);
1465 if (xs == NULL((void *)0))
1466 return ENOMEM12;
1467 xs->cmdlen = sizeof(*cmd);
1468 xs->timeout = 2000;
1469
1470 cmd = (struct scsi_pause *)&xs->cmd;
1471 cmd->opcode = PAUSE0x4b;
1472 cmd->resume = go;
1473
1474 error = scsi_xs_sync(xs);
1475 scsi_xs_put(xs);
1476
1477 return error;
1478}
1479
1480/*
1481 * Get scsi driver to send a "RESET" command
1482 */
1483int
1484cd_reset(struct cd_softc *sc)
1485{
1486 struct scsi_xfer *xs;
1487 int error;
1488
1489 xs = scsi_xs_get(sc->sc_link, SCSI_RESET0x00200);
1490 if (xs == NULL((void *)0))
1491 return ENOMEM12;
1492
1493 xs->timeout = 2000;
1494
1495 error = scsi_xs_sync(xs);
1496 scsi_xs_put(xs);
1497
1498 return error;
1499}
1500
1501/*
1502 * Read subchannel
1503 */
1504int
1505cd_read_subchannel(struct cd_softc *sc, int mode, int format, int track,
1506 struct cd_sub_channel_info *data, int len)
1507{
1508 struct scsi_read_subchannel *cmd;
1509 struct scsi_xfer *xs;
1510 int error;
1511
1512 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN0x00800 | SCSI_SILENT0x00020);
1513 if (xs == NULL((void *)0))
1514 return ENOMEM12;
1515 xs->cmdlen = sizeof(*cmd);
1516 xs->data = (void *)data;
1517 xs->datalen = len;
1518 xs->timeout = 5000;
1519
1520 cmd = (struct scsi_read_subchannel *)&xs->cmd;
1521 cmd->opcode = READ_SUBCHANNEL0x42;
1522 if (mode == CD_MSF_FORMAT2)
1523 SET(cmd->byte2, CD_MSF)((cmd->byte2) |= (0x02));
1524 cmd->byte3 = SRS_SUBQ0x40;
1525 cmd->subchan_format = format;
1526 cmd->track = track;
1527 _lto2b(len, cmd->data_len);
1528
1529 error = scsi_xs_sync(xs);
1530 scsi_xs_put(xs);
1531
1532 return error;
1533}
1534
1535/*
1536 * Read table of contents
1537 */
1538int
1539cd_read_toc(struct cd_softc *sc, int mode, int start, void *data, int len,
1540 int control)
1541{
1542 struct scsi_read_toc *cmd;
1543 struct scsi_xfer *xs;
1544 int error;
1545
1546 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN0x00800 |
1547 SCSI_IGNORE_ILLEGAL_REQUEST0x00100);
1548 if (xs == NULL((void *)0))
1549 return ENOMEM12;
1550 xs->cmdlen = sizeof(*cmd);
1551 xs->data = data;
1552 xs->datalen = len;
1553 xs->timeout = 5000;
1554
1555 bzero(data, len)__builtin_bzero((data), (len));
1556
1557 cmd = (struct scsi_read_toc *)&xs->cmd;
1558 cmd->opcode = READ_TOC0x43;
1559
1560 if (mode == CD_MSF_FORMAT2)
1561 SET(cmd->byte2, CD_MSF)((cmd->byte2) |= (0x02));
1562 cmd->from_track = start;
1563 _lto2b(len, cmd->data_len);
1564 cmd->control = control;
1565
1566 error = scsi_xs_sync(xs);
1567 scsi_xs_put(xs);
1568
1569 return error;
1570}
1571
1572int
1573cd_load_toc(struct cd_softc *sc, struct cd_toc *toc, int fmt)
1574{
1575 int n, len, error;
1576
1577 error = cd_read_toc(sc, 0, 0, toc, sizeof(toc->header), 0);
1578
1579 if (error == 0) {
1580 if (toc->header.ending_track < toc->header.starting_track)
1581 return EIO5;
1582 /* +2 to account for leading out track. */
1583 n = toc->header.ending_track - toc->header.starting_track + 2;
1584 len = n * sizeof(struct cd_toc_entry) + sizeof(toc->header);
1585 error = cd_read_toc(sc, fmt, 0, toc, len, 0);
1586 }
1587
1588 return error;
1589}
1590
1591
1592/*
1593 * Get the scsi driver to send a full inquiry to the device and use the
1594 * results to fill out the disk parameter structure.
1595 */
1596int
1597cd_get_parms(struct cd_softc *sc, int flags)
1598{
1599 /* Reasonable defaults for drives that don't support READ_CAPACITY */
1600 sc->params.secsize = 2048;
1601 sc->params.disksize = 400000;
1602
1603 if (ISSET(sc->sc_link->quirks, ADEV_NOCAPACITY)((sc->sc_link->quirks) & (0x0080)))
1604 return 0;
1605
1606 sc->params.disksize = cd_size(sc->sc_link, flags, &sc->params.secsize);
1607
1608 if ((sc->params.secsize < 512) ||
1609 ((sc->params.secsize & 511) != 0))
1610 sc->params.secsize = 2048; /* some drives lie ! */
1611
1612 if (sc->params.disksize < 100)
1613 sc->params.disksize = 400000;
1614
1615 return 0;
1616}
1617
1618daddr_t
1619cdsize(dev_t dev)
1620{
1621 /* CD-ROMs are read-only. */
1622 return -1;
1623}
1624
1625int
1626cddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1627{
1628 /* Not implemented. */
1629 return ENXIO6;
1630}
1631
1632#define dvd_copy_key(dst, src)__builtin_memcpy(((dst)), ((src)), (5)) memcpy((dst), (src), DVD_KEY_SIZE)__builtin_memcpy(((dst)), ((src)), (5))
1633#define dvd_copy_challenge(dst, src)__builtin_memcpy(((dst)), ((src)), (10)) memcpy((dst), (src), DVD_CHALLENGE_SIZE)__builtin_memcpy(((dst)), ((src)), (10))
1634
1635#define DVD_AUTH_BUFSIZE20 20
1636
1637int
1638dvd_auth(struct cd_softc *sc, union dvd_authinfo *a)
1639{
1640 struct scsi_generic *cmd;
1641 struct scsi_xfer *xs;
1642 u_int8_t *buf;
1643 int error;
1644
1645 buf = dma_alloc(DVD_AUTH_BUFSIZE20, PR_WAITOK0x0001 | PR_ZERO0x0008);
1646 if (buf == NULL((void *)0))
1647 return ENOMEM12;
1648
1649 xs = scsi_xs_get(sc->sc_link, 0);
1650 if (xs == NULL((void *)0)) {
1651 error = ENOMEM12;
1652 goto done;
1653 }
1654 xs->cmdlen = sizeof(*cmd);
1655 xs->timeout = 30000;
1656 xs->data = buf;
1657
1658 cmd = &xs->cmd;
1659
1660 switch (a->type) {
1661 case DVD_LU_SEND_AGID0:
1662 cmd->opcode = GPCMD_REPORT_KEY0xa4;
1663 cmd->bytes[8] = 8;
1664 cmd->bytes[9] = 0 | (0 << 6);
1665 xs->datalen = 8;
1666 SET(xs->flags, SCSI_DATA_IN)((xs->flags) |= (0x00800));
1667
1668 error = scsi_xs_sync(xs);
1669 scsi_xs_put(xs);
1670
1671 if (error == 0)
1672 a->lsa.agid = buf[7] >> 6;
1673 break;
1674
1675 case DVD_LU_SEND_CHALLENGE3:
1676 cmd->opcode = GPCMD_REPORT_KEY0xa4;
1677 cmd->bytes[8] = 16;
1678 cmd->bytes[9] = 1 | (a->lsc.agid << 6);
1679 xs->datalen = 16;
1680 SET(xs->flags, SCSI_DATA_IN)((xs->flags) |= (0x00800));
1681
1682 error = scsi_xs_sync(xs);
1683 scsi_xs_put(xs);
1684 if (error == 0)
1685 dvd_copy_challenge(a->lsc.chal, &buf[4])__builtin_memcpy(((a->lsc.chal)), ((&buf[4])), (10));
1686 break;
1687
1688 case DVD_LU_SEND_KEY12:
1689 cmd->opcode = GPCMD_REPORT_KEY0xa4;
1690 cmd->bytes[8] = 12;
1691 cmd->bytes[9] = 2 | (a->lsk.agid << 6);
1692 xs->datalen = 12;
1693 SET(xs->flags, SCSI_DATA_IN)((xs->flags) |= (0x00800));
1694
1695 error = scsi_xs_sync(xs);
1696 scsi_xs_put(xs);
1697
1698 if (error == 0)
1699 dvd_copy_key(a->lsk.key, &buf[4])__builtin_memcpy(((a->lsk.key)), ((&buf[4])), (5));
1700 break;
1701
1702 case DVD_LU_SEND_TITLE_KEY7:
1703 cmd->opcode = GPCMD_REPORT_KEY0xa4;
1704 _lto4b(a->lstk.lba, &cmd->bytes[1]);
1705 cmd->bytes[8] = 12;
1706 cmd->bytes[9] = 4 | (a->lstk.agid << 6);
1707 xs->datalen = 12;
1708 SET(xs->flags, SCSI_DATA_IN)((xs->flags) |= (0x00800));
1709
1710 error = scsi_xs_sync(xs);
1711 scsi_xs_put(xs);
1712
1713 if (error == 0) {
1714 a->lstk.cpm = (buf[4] >> 7) & 1;
1715 a->lstk.cp_sec = (buf[4] >> 6) & 1;
1716 a->lstk.cgms = (buf[4] >> 4) & 3;
1717 dvd_copy_key(a->lstk.title_key, &buf[5])__builtin_memcpy(((a->lstk.title_key)), ((&buf[5])), (
5))
;
1718 }
1719 break;
1720
1721 case DVD_LU_SEND_ASF8:
1722 cmd->opcode = GPCMD_REPORT_KEY0xa4;
1723 cmd->bytes[8] = 8;
1724 cmd->bytes[9] = 5 | (a->lsasf.agid << 6);
1725 xs->datalen = 8;
1726 SET(xs->flags, SCSI_DATA_IN)((xs->flags) |= (0x00800));
1727
1728 error = scsi_xs_sync(xs);
1729 scsi_xs_put(xs);
1730
1731 if (error == 0)
1732 a->lsasf.asf = buf[7] & 1;
1733 break;
1734
1735 case DVD_HOST_SEND_CHALLENGE1:
1736 cmd->opcode = GPCMD_SEND_KEY0xa3;
1737 cmd->bytes[8] = 16;
1738 cmd->bytes[9] = 1 | (a->hsc.agid << 6);
1739 buf[1] = 14;
1740 dvd_copy_challenge(&buf[4], a->hsc.chal)__builtin_memcpy(((&buf[4])), ((a->hsc.chal)), (10));
1741 xs->datalen = 16;
1742 SET(xs->flags, SCSI_DATA_OUT)((xs->flags) |= (0x01000));
1743
1744 error = scsi_xs_sync(xs);
1745 scsi_xs_put(xs);
1746
1747 if (error == 0)
1748 a->type = DVD_LU_SEND_KEY12;
1749 break;
1750
1751 case DVD_HOST_SEND_KEY24:
1752 cmd->opcode = GPCMD_SEND_KEY0xa3;
1753 cmd->bytes[8] = 12;
1754 cmd->bytes[9] = 3 | (a->hsk.agid << 6);
1755 buf[1] = 10;
1756 dvd_copy_key(&buf[4], a->hsk.key)__builtin_memcpy(((&buf[4])), ((a->hsk.key)), (5));
1757 xs->datalen = 12;
1758 SET(xs->flags, SCSI_DATA_OUT)((xs->flags) |= (0x01000));
1759
1760 error = scsi_xs_sync(xs);
1761 scsi_xs_put(xs);
1762
1763 if (error == 0)
1764 a->type = DVD_AUTH_ESTABLISHED5;
1765 else
1766 a->type = DVD_AUTH_FAILURE6;
1767 break;
1768
1769 case DVD_INVALIDATE_AGID9:
1770 cmd->opcode = GPCMD_REPORT_KEY0xa4;
1771 cmd->bytes[9] = 0x3f | (a->lsa.agid << 6);
1772 xs->data = NULL((void *)0);
1773
1774 error = scsi_xs_sync(xs);
1775 scsi_xs_put(xs);
1776 break;
1777
1778 case DVD_LU_SEND_RPC_STATE10:
1779 cmd->opcode = GPCMD_REPORT_KEY0xa4;
1780 cmd->bytes[8] = 8;
1781 cmd->bytes[9] = 8 | (0 << 6);
1782 xs->datalen = 8;
1783 SET(xs->flags, SCSI_DATA_IN)((xs->flags) |= (0x00800));
1784
1785 error = scsi_xs_sync(xs);
1786 scsi_xs_put(xs);
1787
1788 if (error == 0) {
1789 a->lrpcs.type = (buf[4] >> 6) & 3;
1790 a->lrpcs.vra = (buf[4] >> 3) & 7;
1791 a->lrpcs.ucca = (buf[4]) & 7;
1792 a->lrpcs.region_mask = buf[5];
1793 a->lrpcs.rpc_scheme = buf[6];
1794 }
1795 break;
1796
1797 case DVD_HOST_SEND_RPC_STATE11:
1798 cmd->opcode = GPCMD_SEND_KEY0xa3;
1799 cmd->bytes[8] = 8;
1800 cmd->bytes[9] = 6 | (0 << 6);
1801 buf[1] = 6;
1802 buf[4] = a->hrpcs.pdrc;
1803 xs->datalen = 8;
1804 SET(xs->flags, SCSI_DATA_OUT)((xs->flags) |= (0x01000));
1805
1806 error = scsi_xs_sync(xs);
1807 scsi_xs_put(xs);
1808 break;
1809
1810 default:
1811 scsi_xs_put(xs);
1812 error = ENOTTY25;
1813 break;
1814 }
1815done:
1816 dma_free(buf, DVD_AUTH_BUFSIZE20);
1817 return error;
1818}
1819
1820#define DVD_READ_PHYSICAL_BUFSIZE(4 + 4 * 20) (4 + 4 * 20)
1821int
1822dvd_read_physical(struct cd_softc *sc, union dvd_struct *s)
1823{
1824 struct scsi_generic *cmd;
1825 struct dvd_layer *layer;
1826 struct scsi_xfer *xs;
1827 u_int8_t *buf, *bufp;
1828 int error, i;
1829
1830 buf = dma_alloc(DVD_READ_PHYSICAL_BUFSIZE(4 + 4 * 20), PR_WAITOK0x0001 | PR_ZERO0x0008);
1831 if (buf == NULL((void *)0))
1832 return ENOMEM12;
1833
1834 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN0x00800);
1835 if (xs == NULL((void *)0)) {
1836 error = ENOMEM12;
1837 goto done;
1838 }
1839 xs->cmdlen = sizeof(*cmd);
1840 xs->data = buf;
1841 xs->datalen = DVD_READ_PHYSICAL_BUFSIZE(4 + 4 * 20);
1842 xs->timeout = 30000;
1843
1844 cmd = &xs->cmd;
1845 cmd->opcode = GPCMD_READ_DVD_STRUCTURE0xad;
1846 cmd->bytes[6] = s->type;
1847 _lto2b(xs->datalen, &cmd->bytes[7]);
1848
1849 cmd->bytes[5] = s->physical.layer_num;
1850
1851 error = scsi_xs_sync(xs);
1852 scsi_xs_put(xs);
1853
1854 if (error == 0) {
1855 for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0];
1856 i < 4; i++, bufp += 20, layer++) {
1857 bzero(layer, sizeof(*layer))__builtin_bzero((layer), (sizeof(*layer)));
1858 layer->book_version = bufp[0] & 0xf;
1859 layer->book_type = bufp[0] >> 4;
1860 layer->min_rate = bufp[1] & 0xf;
1861 layer->disc_size = bufp[1] >> 4;
1862 layer->layer_type = bufp[2] & 0xf;
1863 layer->track_path = (bufp[2] >> 4) & 1;
1864 layer->nlayers = (bufp[2] >> 5) & 3;
1865 layer->track_density = bufp[3] & 0xf;
1866 layer->linear_density = bufp[3] >> 4;
1867 layer->start_sector = _4btol(&bufp[4]);
1868 layer->end_sector = _4btol(&bufp[8]);
1869 layer->end_sector_l0 = _4btol(&bufp[12]);
1870 layer->bca = bufp[16] >> 7;
1871 }
1872 }
1873done:
1874 dma_free(buf, DVD_READ_PHYSICAL_BUFSIZE(4 + 4 * 20));
1875 return error;
1876}
1877
1878#define DVD_READ_COPYRIGHT_BUFSIZE8 8
1879int
1880dvd_read_copyright(struct cd_softc *sc, union dvd_struct *s)
1881{
1882 struct scsi_generic *cmd;
1883 struct scsi_xfer *xs;
1884 u_int8_t *buf;
1885 int error;
1886
1887 buf = dma_alloc(DVD_READ_COPYRIGHT_BUFSIZE8, PR_WAITOK0x0001 | PR_ZERO0x0008);
1888 if (buf == NULL((void *)0))
1889 return ENOMEM12;
1890
1891 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN0x00800);
1892 if (xs == NULL((void *)0)) {
1893 error = ENOMEM12;
1894 goto done;
1895 }
1896 xs->cmdlen = sizeof(*cmd);
1897 xs->data = buf;
1898 xs->datalen = DVD_READ_COPYRIGHT_BUFSIZE8;
1899 xs->timeout = 30000;
1900
1901 cmd = &xs->cmd;
1902 cmd->opcode = GPCMD_READ_DVD_STRUCTURE0xad;
1903 cmd->bytes[6] = s->type;
1904 _lto2b(xs->datalen, &cmd->bytes[7]);
1905
1906 cmd->bytes[5] = s->copyright.layer_num;
1907
1908 error = scsi_xs_sync(xs);
1909 scsi_xs_put(xs);
1910
1911 if (error == 0) {
1912 s->copyright.cpst = buf[4];
1913 s->copyright.rmi = buf[5];
1914 }
1915done:
1916 dma_free(buf, DVD_READ_COPYRIGHT_BUFSIZE8);
1917 return error;
1918}
1919
1920int
1921dvd_read_disckey(struct cd_softc *sc, union dvd_struct *s)
1922{
1923 struct scsi_read_dvd_structure_data *buf;
1924 struct scsi_read_dvd_structure *cmd;
1925 struct scsi_xfer *xs;
1926 int error;
1927
1928 buf = dma_alloc(sizeof(*buf), PR_WAITOK0x0001 | PR_ZERO0x0008);
1929 if (buf == NULL((void *)0))
1930 return ENOMEM12;
1931
1932 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN0x00800);
1933 if (xs == NULL((void *)0)) {
1934 error = ENOMEM12;
1935 goto done;
1936 }
1937 xs->cmdlen = sizeof(*cmd);
1938 xs->data = (void *)buf;
1939 xs->datalen = sizeof(*buf);
1940 xs->timeout = 30000;
1941
1942 cmd = (struct scsi_read_dvd_structure *)&xs->cmd;
1943 cmd->opcode = GPCMD_READ_DVD_STRUCTURE0xad;
1944 cmd->format = s->type;
1945 cmd->agid = s->disckey.agid << 6;
1946 _lto2b(xs->datalen, cmd->length);
1947
1948 error = scsi_xs_sync(xs);
1949 scsi_xs_put(xs);
1950
1951 if (error == 0)
1952 memcpy(s->disckey.value, buf->data, sizeof(s->disckey.value))__builtin_memcpy((s->disckey.value), (buf->data), (sizeof
(s->disckey.value)))
;
1953done:
1954 dma_free(buf, sizeof(*buf));
1955 return error;
1956}
1957
1958#define DVD_READ_BCA_BUFLEN(4 + 188) (4 + 188)
1959
1960int
1961dvd_read_bca(struct cd_softc *sc, union dvd_struct *s)
1962{
1963 struct scsi_generic *cmd;
1964 struct scsi_xfer *xs;
1965 u_int8_t *buf;
1966 int error;
1967
1968 buf = dma_alloc(DVD_READ_BCA_BUFLEN(4 + 188), PR_WAITOK0x0001 | PR_ZERO0x0008);
1969 if (buf == NULL((void *)0))
1970 return ENOMEM12;
1971
1972 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN0x00800);
1973 if (xs == NULL((void *)0)) {
1974 error = ENOMEM12;
1975 goto done;
1976 }
1977 xs->cmdlen = sizeof(*cmd);
1978 xs->data = buf;
1979 xs->datalen = DVD_READ_BCA_BUFLEN(4 + 188);
1980 xs->timeout = 30000;
1981
1982 cmd = &xs->cmd;
1983 cmd->opcode = GPCMD_READ_DVD_STRUCTURE0xad;
1984 cmd->bytes[6] = s->type;
1985 _lto2b(xs->datalen, &cmd->bytes[7]);
1986
1987 error = scsi_xs_sync(xs);
1988 scsi_xs_put(xs);
1989
1990 if (error == 0) {
1991 s->bca.len = _2btol(&buf[0]);
1992 if (s->bca.len < 12 || s->bca.len > 188)
1993 return EIO5;
1994 memcpy(s->bca.value, &buf[4], s->bca.len)__builtin_memcpy((s->bca.value), (&buf[4]), (s->bca
.len))
;
1995 }
1996done:
1997 dma_free(buf, DVD_READ_BCA_BUFLEN(4 + 188));
1998 return error;
1999}
2000
2001int
2002dvd_read_manufact(struct cd_softc *sc, union dvd_struct *s)
2003{
2004 struct scsi_read_dvd_structure_data *buf;
2005 struct scsi_read_dvd_structure *cmd;
2006 struct scsi_xfer *xs;
2007 int error;
2008
2009 buf = dma_alloc(sizeof(*buf), PR_WAITOK0x0001 | PR_ZERO0x0008);
2010 if (buf == NULL((void *)0))
2011 return ENOMEM12;
2012
2013 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN0x00800);
2014 if (xs == NULL((void *)0)) {
2015 error = ENOMEM12;
2016 goto done;
2017 }
2018 xs->cmdlen = sizeof(*cmd);
2019 xs->data = (void *)buf;
2020 xs->datalen = sizeof(*buf);
2021 xs->timeout = 30000;
2022
2023 cmd = (struct scsi_read_dvd_structure *)&xs->cmd;
2024 cmd->opcode = GPCMD_READ_DVD_STRUCTURE0xad;
2025 cmd->format = s->type;
2026 _lto2b(xs->datalen, cmd->length);
2027
2028 error = scsi_xs_sync(xs);
2029 scsi_xs_put(xs);
2030
2031 if (error == 0) {
2032 s->manufact.len = _2btol(buf->len);
2033 if (s->manufact.len >= 0 && s->manufact.len <= 2048)
2034 memcpy(s->manufact.value, buf->data, s->manufact.len)__builtin_memcpy((s->manufact.value), (buf->data), (s->
manufact.len))
;
2035 else
2036 error = EIO5;
2037 }
2038done:
2039 dma_free(buf, sizeof(*buf));
2040 return error;
2041}
2042
2043int
2044dvd_read_struct(struct cd_softc *sc, union dvd_struct *s)
2045{
2046 switch (s->type) {
2047 case DVD_STRUCT_PHYSICAL0x00:
2048 return dvd_read_physical(sc, s);
2049 case DVD_STRUCT_COPYRIGHT0x01:
2050 return dvd_read_copyright(sc, s);
2051 case DVD_STRUCT_DISCKEY0x02:
2052 return dvd_read_disckey(sc, s);
2053 case DVD_STRUCT_BCA0x03:
2054 return dvd_read_bca(sc, s);
2055 case DVD_STRUCT_MANUFACT0x04:
2056 return dvd_read_manufact(sc, s);
2057 default:
2058 return EINVAL22;
2059 }
2060}
2061
2062int
2063cd_interpret_sense(struct scsi_xfer *xs)
2064{
2065 struct scsi_sense_data *sense = &xs->sense;
2066 struct scsi_link *link = xs->sc_link;
2067 u_int8_t skey = sense->flags & SSD_KEY0x0F;
2068 u_int8_t serr = sense->error_code & SSD_ERRCODE0x7F;
2069
2070 if (!ISSET(link->flags, SDEV_OPEN)((link->flags) & (0x0008)) ||
2071 (serr != SSD_ERRCODE_CURRENT0x70 && serr != SSD_ERRCODE_DEFERRED0x71))
2072 return scsi_interpret_sense(xs);
2073
2074 /*
2075 * We do custom processing in cd for the unit becoming ready
2076 * case. We do not allow xs->retries to be decremented on the
2077 * "Unit Becoming Ready" case. This is because CD drives
2078 * report "Unit Becoming Ready" when loading media and can
2079 * take a long time. Rather than having a massive timeout for
2080 * all operations (which would cause other problems), we allow
2081 * operations to wait (but be interruptible with Ctrl-C)
2082 * forever as long as the drive is reporting that it is
2083 * becoming ready. All other cases of not being ready are
2084 * handled by the default handler.
2085 */
2086 switch(skey) {
2087 case SKEY_NOT_READY0x02:
2088 if (ISSET(xs->flags, SCSI_IGNORE_NOT_READY)((xs->flags) & (0x00040)))
2089 return 0;
2090 if (ASC_ASCQ(sense)((sense->add_sense_code << 8) | sense->add_sense_code_qual
)
== SENSE_NOT_READY_BECOMING_READY0x0401) {
2091 SC_DEBUG(link, SDEV_DB1, ("not ready: busy (%#x)\n",
2092 sense->add_sense_code_qual));
2093 /* don't count this as a retry */
2094 xs->retries++;
2095 return scsi_delay(xs, 1);
2096 }
2097 break;
2098 /* XXX more to come here for a few other cases */
2099 default:
2100 break;
2101 }
2102 return scsi_interpret_sense(xs);
2103}
2104
2105/*
2106 * Find out from the device what its capacity is.
2107 */
2108u_int64_t
2109cd_size(struct scsi_link *link, int flags, u_int32_t *blksize)
2110{
2111 struct scsi_read_cap_data_16 *rdcap16;
2112 struct scsi_read_cap_data *rdcap;
2113 u_int64_t max_addr;
2114 int error;
2115
2116 if (blksize != NULL((void *)0))
2117 *blksize = 0;
2118
2119 CLR(flags, SCSI_IGNORE_ILLEGAL_REQUEST)((flags) &= ~(0x00100));
2120
2121 /*
2122 * Start with a READ CAPACITY(10).
2123 */
2124 rdcap = dma_alloc(sizeof(*rdcap), ((flags & SCSI_NOSLEEP0x00001) ?
2125 PR_NOWAIT0x0002 : PR_WAITOK0x0001) | PR_ZERO0x0008);
2126 if (rdcap == NULL((void *)0))
2127 return 0;
2128
2129 error = scsi_read_cap_10(link, rdcap, flags);
2130 if (error) {
2131 dma_free(rdcap, sizeof(*rdcap));
2132 return 0;
2133 }
2134
2135 max_addr = _4btol(rdcap->addr);
2136 if (blksize != NULL((void *)0))
2137 *blksize = _4btol(rdcap->length);
2138 dma_free(rdcap, sizeof(*rdcap));
2139
2140 /*
2141 * pre-SPC (i.e. pre-SCSI-3) devices reporting less than 2^32-1 sectors
2142 * can stop here.
2143 */
2144 if (SID_ANSII_REV(&link->inqdata)((&link->inqdata)->version & 0x07) < SCSI_REV_SPC0x03 &&
2145 max_addr != 0xffffffff)
2146 goto exit;
2147
2148 rdcap16 = dma_alloc(sizeof(*rdcap16), ((flags & SCSI_NOSLEEP0x00001) ?
2149 PR_NOWAIT0x0002 : PR_WAITOK0x0001) | PR_ZERO0x0008);
2150 if (rdcap16 == NULL((void *)0))
2151 goto exit;
2152
2153 error = scsi_read_cap_16(link, rdcap16, flags);
2154 if (error) {
2155 dma_free(rdcap16, sizeof(*rdcap16));
2156 goto exit;
2157 }
2158
2159 max_addr = _8btol(rdcap16->addr);
2160 if (blksize != NULL((void *)0))
2161 *blksize = _4btol(rdcap16->length);
2162 /* XXX The other READ CAPACITY(16) info could be stored away. */
2163 dma_free(rdcap16, sizeof(*rdcap16));
2164
2165 return max_addr + 1;
2166
2167exit:
2168 /* Return READ CAPACITY 10 values. */
2169 if (max_addr != 0xffffffff)
2170 return max_addr + 1;
2171 else if (blksize != NULL((void *)0))
2172 *blksize = 0;
2173 return 0;
2174}
2175
2176#if defined(__macppc__)
2177int
2178cd_eject(void)
2179{
2180 struct cd_softc *sc;
2181 int error = 0;
2182
2183 if (cd_cd.cd_ndevs == 0 || (sc = cd_cd.cd_devs[0]) == NULL((void *)0))
2184 return ENXIO6;
2185
2186 if ((error = disk_lock(&sc->sc_dk)) != 0)
2187 return error;
2188
2189 if (sc->sc_dk.dk_openmask == 0) {
2190 SET(sc->sc_link->flags, SDEV_EJECTING)((sc->sc_link->flags) |= (0x0100));
2191
2192 scsi_prevent(sc->sc_link, PR_ALLOW0x00,
2193 SCSI_IGNORE_ILLEGAL_REQUEST0x00100 | SCSI_IGNORE_NOT_READY0x00040 |
2194 SCSI_SILENT0x00020 | SCSI_IGNORE_MEDIA_CHANGE0x00080);
2195 CLR(sc->sc_link->flags, SDEV_MEDIA_LOADED)((sc->sc_link->flags) &= ~(0x0002));
2196
2197 scsi_start(sc->sc_link, SSS_STOP0x00|SSS_LOEJ0x02, 0);
2198
2199 CLR(sc->sc_link->flags, SDEV_EJECTING)((sc->sc_link->flags) &= ~(0x0100));
2200 }
2201 disk_unlock(&sc->sc_dk);
2202
2203 return error;
2204}
2205#endif /* __macppc__ */