cd.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259
  1. /* $OpenBSD: cd.c,v 1.217 2015/06/07 19:13:27 krw Exp $ */
  2. /* $NetBSD: cd.c,v 1.100 1997/04/02 02:29:30 mycroft Exp $ */
  3. /*
  4. * Copyright (c) 1994, 1995, 1997 Charles M. Hannum. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by Charles M. Hannum.
  17. * 4. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * Originally written by Julian Elischer (julian@tfs.com)
  33. * for TRW Financial Systems for use under the MACH(2.5) operating system.
  34. *
  35. * TRW Financial Systems, in accordance with their agreement with Carnegie
  36. * Mellon University, makes this software available to CMU to distribute
  37. * or use in any manner that they see fit as long as this message is kept with
  38. * the software. For this reason TFS also grants any other persons or
  39. * organisations permission to use or modify this software.
  40. *
  41. * TFS supplies this software to be publicly redistributed
  42. * on the understanding that TFS is not responsible for the correct
  43. * functioning of this software in any circumstances.
  44. *
  45. * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
  46. */
  47. #include <sys/types.h>
  48. #include <sys/param.h>
  49. #include <sys/systm.h>
  50. #include <sys/timeout.h>
  51. #include <sys/file.h>
  52. #include <sys/stat.h>
  53. #include <sys/ioctl.h>
  54. #include <sys/mtio.h>
  55. #include <sys/buf.h>
  56. #include <sys/uio.h>
  57. #include <sys/malloc.h>
  58. #include <sys/pool.h>
  59. #include <sys/errno.h>
  60. #include <sys/device.h>
  61. #include <sys/disklabel.h>
  62. #include <sys/disk.h>
  63. #include <sys/cdio.h>
  64. #include <sys/conf.h>
  65. #include <sys/scsiio.h>
  66. #include <sys/dkio.h>
  67. #include <sys/vnode.h>
  68. #include <scsi/scsi_all.h>
  69. #include <scsi/cd.h>
  70. #include <scsi/scsi_disk.h> /* rw_big and start_stop come from there */
  71. #include <scsi/scsiconf.h>
  72. #include <ufs/ffs/fs.h> /* for BBSIZE and SBSIZE */
  73. #define CDOUTSTANDING 4
  74. #define MAXTRACK 99
  75. #define CD_FRAMES 75
  76. #define CD_SECS 60
  77. struct cd_toc {
  78. struct ioc_toc_header header;
  79. struct cd_toc_entry entries[MAXTRACK+1]; /* One extra for the */
  80. /* leadout */
  81. };
  82. int cdmatch(struct device *, void *, void *);
  83. void cdattach(struct device *, struct device *, void *);
  84. int cdactivate(struct device *, int);
  85. int cddetach(struct device *, int);
  86. struct cd_softc {
  87. struct device sc_dev;
  88. struct disk sc_dk;
  89. int sc_flags;
  90. #define CDF_ANCIENT 0x10 /* disk is ancient; for minphys */
  91. #define CDF_DYING 0x40 /* dying, when deactivated */
  92. #define CDF_WAITING 0x100
  93. struct scsi_link *sc_link; /* contains our targ, lun, etc. */
  94. struct cd_parms {
  95. u_int32_t secsize;
  96. u_int64_t disksize; /* total number sectors */
  97. } params;
  98. struct bufq sc_bufq;
  99. struct scsi_xshandler sc_xsh;
  100. struct timeout sc_timeout;
  101. };
  102. void cdstart(struct scsi_xfer *);
  103. void cd_buf_done(struct scsi_xfer *);
  104. void cdminphys(struct buf *);
  105. int cdgetdisklabel(dev_t, struct cd_softc *, struct disklabel *, int);
  106. int cd_setchan(struct cd_softc *, int, int, int, int, int);
  107. int cd_getvol(struct cd_softc *cd, struct ioc_vol *, int);
  108. int cd_setvol(struct cd_softc *, const struct ioc_vol *, int);
  109. int cd_load_unload(struct cd_softc *, int, int);
  110. int cd_set_pa_immed(struct cd_softc *, int);
  111. int cd_play(struct cd_softc *, int, int);
  112. int cd_play_tracks(struct cd_softc *, int, int, int, int);
  113. int cd_play_msf(struct cd_softc *, int, int, int, int, int, int);
  114. int cd_pause(struct cd_softc *, int);
  115. int cd_reset(struct cd_softc *);
  116. int cd_read_subchannel(struct cd_softc *, int, int, int,
  117. struct cd_sub_channel_info *, int );
  118. int cd_read_toc(struct cd_softc *, int, int, void *, int, int);
  119. int cd_get_parms(struct cd_softc *, int);
  120. int cd_load_toc(struct cd_softc *, struct cd_toc *, int);
  121. int cd_interpret_sense(struct scsi_xfer *);
  122. u_int64_t cd_size(struct scsi_link *, int, u_int32_t *);
  123. int dvd_auth(struct cd_softc *, union dvd_authinfo *);
  124. int dvd_read_physical(struct cd_softc *, union dvd_struct *);
  125. int dvd_read_copyright(struct cd_softc *, union dvd_struct *);
  126. int dvd_read_disckey(struct cd_softc *, union dvd_struct *);
  127. int dvd_read_bca(struct cd_softc *, union dvd_struct *);
  128. int dvd_read_manufact(struct cd_softc *, union dvd_struct *);
  129. int dvd_read_struct(struct cd_softc *, union dvd_struct *);
  130. #if defined(__macppc__)
  131. int cd_eject(void);
  132. #endif
  133. struct cfattach cd_ca = {
  134. sizeof(struct cd_softc), cdmatch, cdattach,
  135. cddetach, cdactivate
  136. };
  137. struct cfdriver cd_cd = {
  138. NULL, "cd", DV_DISK
  139. };
  140. const struct scsi_inquiry_pattern cd_patterns[] = {
  141. {T_CDROM, T_REMOV,
  142. "", "", ""},
  143. {T_CDROM, T_FIXED,
  144. "", "", ""},
  145. {T_WORM, T_REMOV,
  146. "", "", ""},
  147. {T_WORM, T_FIXED,
  148. "", "", ""},
  149. {T_DIRECT, T_REMOV,
  150. "NEC CD-ROM DRIVE:260", "", ""},
  151. #if 0
  152. {T_CDROM, T_REMOV, /* more luns */
  153. "PIONEER ", "CD-ROM DRM-600 ", ""},
  154. #endif
  155. };
  156. #define cdlookup(unit) (struct cd_softc *)disk_lookup(&cd_cd, (unit))
  157. int
  158. cdmatch(struct device *parent, void *match, void *aux)
  159. {
  160. struct scsi_attach_args *sa = aux;
  161. int priority;
  162. scsi_inqmatch(sa->sa_inqbuf, cd_patterns, nitems(cd_patterns),
  163. sizeof(cd_patterns[0]), &priority);
  164. return (priority);
  165. }
  166. /*
  167. * The routine called by the low level scsi routine when it discovers
  168. * A device suitable for this driver
  169. */
  170. void
  171. cdattach(struct device *parent, struct device *self, void *aux)
  172. {
  173. struct cd_softc *sc = (struct cd_softc *)self;
  174. struct scsi_attach_args *sa = aux;
  175. struct scsi_link *sc_link = sa->sa_sc_link;
  176. SC_DEBUG(sc_link, SDEV_DB2, ("cdattach:\n"));
  177. /*
  178. * Store information needed to contact our base driver
  179. */
  180. sc->sc_link = sc_link;
  181. sc_link->interpret_sense = cd_interpret_sense;
  182. sc_link->device_softc = sc;
  183. if (sc_link->openings > CDOUTSTANDING)
  184. sc_link->openings = CDOUTSTANDING;
  185. /*
  186. * Initialize disk structures.
  187. */
  188. sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
  189. bufq_init(&sc->sc_bufq, BUFQ_DEFAULT);
  190. /*
  191. * Note if this device is ancient. This is used in cdminphys().
  192. */
  193. if (!(sc_link->flags & SDEV_ATAPI) &&
  194. SCSISPC(sa->sa_inqbuf->version) == 0)
  195. sc->sc_flags |= CDF_ANCIENT;
  196. printf("\n");
  197. scsi_xsh_set(&sc->sc_xsh, sc_link, cdstart);
  198. timeout_set(&sc->sc_timeout, (void (*)(void *))scsi_xsh_add,
  199. &sc->sc_xsh);
  200. /* Attach disk. */
  201. sc->sc_dk.dk_flags = DKF_NOLABELREAD;
  202. disk_attach(&sc->sc_dev, &sc->sc_dk);
  203. }
  204. int
  205. cdactivate(struct device *self, int act)
  206. {
  207. struct cd_softc *sc = (struct cd_softc *)self;
  208. int rv = 0;
  209. switch (act) {
  210. case DVACT_RESUME:
  211. /*
  212. * When resuming, hardware may have forgotten we locked it. So if
  213. * there are any open partitions, lock the CD.
  214. */
  215. if (sc->sc_dk.dk_openmask != 0)
  216. scsi_prevent(sc->sc_link, PR_PREVENT,
  217. SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE |
  218. SCSI_SILENT | SCSI_AUTOCONF);
  219. break;
  220. case DVACT_DEACTIVATE:
  221. sc->sc_flags |= CDF_DYING;
  222. scsi_xsh_del(&sc->sc_xsh);
  223. break;
  224. }
  225. return (rv);
  226. }
  227. int
  228. cddetach(struct device *self, int flags)
  229. {
  230. struct cd_softc *sc = (struct cd_softc *)self;
  231. bufq_drain(&sc->sc_bufq);
  232. disk_gone(cdopen, self->dv_unit);
  233. /* Detach disk. */
  234. bufq_destroy(&sc->sc_bufq);
  235. disk_detach(&sc->sc_dk);
  236. return (0);
  237. }
  238. /*
  239. * Open the device. Make sure the partition info is as up-to-date as can be.
  240. */
  241. int
  242. cdopen(dev_t dev, int flag, int fmt, struct proc *p)
  243. {
  244. struct scsi_link *sc_link;
  245. struct cd_softc *sc;
  246. int error = 0, part, rawopen, unit;
  247. unit = DISKUNIT(dev);
  248. part = DISKPART(dev);
  249. rawopen = (part == RAW_PART) && (fmt == S_IFCHR);
  250. sc = cdlookup(unit);
  251. if (sc == NULL)
  252. return (ENXIO);
  253. if (sc->sc_flags & CDF_DYING) {
  254. device_unref(&sc->sc_dev);
  255. return (ENXIO);
  256. }
  257. sc_link = sc->sc_link;
  258. SC_DEBUG(sc_link, SDEV_DB1,
  259. ("cdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
  260. cd_cd.cd_ndevs, part));
  261. if ((error = disk_lock(&sc->sc_dk)) != 0) {
  262. device_unref(&sc->sc_dev);
  263. return (error);
  264. }
  265. if (sc->sc_dk.dk_openmask != 0) {
  266. /*
  267. * If any partition is open, but the disk has been invalidated,
  268. * disallow further opens.
  269. */
  270. if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
  271. if (rawopen)
  272. goto out;
  273. error = EIO;
  274. goto bad;
  275. }
  276. } else {
  277. /*
  278. * Check that it is still responding and ok. Drive can be in
  279. * progress of loading media so use increased retries number
  280. * and don't ignore NOT_READY.
  281. */
  282. /* Use cd_interpret_sense() now. */
  283. sc_link->flags |= SDEV_OPEN;
  284. error = scsi_test_unit_ready(sc_link, TEST_READY_RETRIES,
  285. (rawopen ? SCSI_SILENT : 0) | SCSI_IGNORE_ILLEGAL_REQUEST |
  286. SCSI_IGNORE_MEDIA_CHANGE);
  287. /* Start the cd spinning if necessary. */
  288. if (error == EIO)
  289. error = scsi_start(sc_link, SSS_START,
  290. SCSI_IGNORE_ILLEGAL_REQUEST |
  291. SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT);
  292. if (error) {
  293. if (rawopen) {
  294. error = 0;
  295. goto out;
  296. } else
  297. goto bad;
  298. }
  299. /* Lock the cd in. */
  300. error = scsi_prevent(sc_link, PR_PREVENT,
  301. SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE |
  302. SCSI_SILENT);
  303. if (error)
  304. goto bad;
  305. /* Load the physical device parameters. */
  306. sc_link->flags |= SDEV_MEDIA_LOADED;
  307. if (cd_get_parms(sc, (rawopen ? SCSI_SILENT : 0) |
  308. SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE)) {
  309. sc_link->flags &= ~SDEV_MEDIA_LOADED;
  310. error = ENXIO;
  311. goto bad;
  312. }
  313. SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded\n"));
  314. /* Fabricate a disk label. */
  315. cdgetdisklabel(dev, sc, sc->sc_dk.dk_label, 0);
  316. SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel fabricated\n"));
  317. }
  318. out:
  319. if ((error = disk_openpart(&sc->sc_dk, part, fmt, 1)) != 0)
  320. goto bad;
  321. sc_link->flags |= SDEV_OPEN;
  322. SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
  323. /* It's OK to fall through because dk_openmask is now non-zero. */
  324. bad:
  325. if (sc->sc_dk.dk_openmask == 0) {
  326. scsi_prevent(sc_link, PR_ALLOW,
  327. SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE |
  328. SCSI_SILENT);
  329. sc_link->flags &= ~(SDEV_OPEN | SDEV_MEDIA_LOADED);
  330. }
  331. disk_unlock(&sc->sc_dk);
  332. device_unref(&sc->sc_dev);
  333. return (error);
  334. }
  335. /*
  336. * Close the device. Only called if we are the last occurrence of an open
  337. * device.
  338. */
  339. int
  340. cdclose(dev_t dev, int flag, int fmt, struct proc *p)
  341. {
  342. struct cd_softc *sc;
  343. int part = DISKPART(dev);
  344. sc = cdlookup(DISKUNIT(dev));
  345. if (sc == NULL)
  346. return ENXIO;
  347. if (sc->sc_flags & CDF_DYING) {
  348. device_unref(&sc->sc_dev);
  349. return (ENXIO);
  350. }
  351. disk_lock_nointr(&sc->sc_dk);
  352. disk_closepart(&sc->sc_dk, part, fmt);
  353. if (sc->sc_dk.dk_openmask == 0) {
  354. /* XXXX Must wait for I/O to complete! */
  355. scsi_prevent(sc->sc_link, PR_ALLOW,
  356. SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY |
  357. SCSI_SILENT);
  358. sc->sc_link->flags &= ~(SDEV_OPEN | SDEV_MEDIA_LOADED);
  359. if (sc->sc_link->flags & SDEV_EJECTING) {
  360. scsi_start(sc->sc_link, SSS_STOP|SSS_LOEJ, 0);
  361. sc->sc_link->flags &= ~SDEV_EJECTING;
  362. }
  363. timeout_del(&sc->sc_timeout);
  364. scsi_xsh_del(&sc->sc_xsh);
  365. }
  366. disk_unlock(&sc->sc_dk);
  367. device_unref(&sc->sc_dev);
  368. return 0;
  369. }
  370. /*
  371. * Actually translate the requested transfer into one the physical driver can
  372. * understand. The transfer is described by a buf and will include only one
  373. * physical transfer.
  374. */
  375. void
  376. cdstrategy(struct buf *bp)
  377. {
  378. struct cd_softc *sc;
  379. int s;
  380. sc = cdlookup(DISKUNIT(bp->b_dev));
  381. if (sc == NULL) {
  382. bp->b_error = ENXIO;
  383. goto bad;
  384. }
  385. if (sc->sc_flags & CDF_DYING) {
  386. bp->b_error = ENXIO;
  387. goto bad;
  388. }
  389. SC_DEBUG(sc->sc_link, SDEV_DB2, ("cdstrategy: %ld bytes @ blk %lld\n",
  390. bp->b_bcount, (long long)bp->b_blkno));
  391. /*
  392. * If the device has been made invalid, error out
  393. * maybe the media changed, or no media loaded
  394. */
  395. if ((sc->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
  396. bp->b_error = EIO;
  397. goto bad;
  398. }
  399. /* Validate the request. */
  400. if (bounds_check_with_label(bp, sc->sc_dk.dk_label) == -1)
  401. goto done;
  402. /* Place it in the queue of disk activities for this disk. */
  403. bufq_queue(&sc->sc_bufq, bp);
  404. /*
  405. * Tell the device to get going on the transfer if it's
  406. * not doing anything, otherwise just wait for completion
  407. */
  408. scsi_xsh_add(&sc->sc_xsh);
  409. device_unref(&sc->sc_dev);
  410. return;
  411. bad:
  412. bp->b_flags |= B_ERROR;
  413. bp->b_resid = bp->b_bcount;
  414. done:
  415. s = splbio();
  416. biodone(bp);
  417. splx(s);
  418. if (sc != NULL)
  419. device_unref(&sc->sc_dev);
  420. }
  421. /*
  422. * cdstart looks to see if there is a buf waiting for the device
  423. * and that the device is not already busy. If both are true,
  424. * It deques the buf and creates a scsi command to perform the
  425. * transfer in the buf. The transfer request will call scsi_done
  426. * on completion, which will in turn call this routine again
  427. * so that the next queued transfer is performed.
  428. * The bufs are queued by the strategy routine (cdstrategy)
  429. *
  430. * This routine is also called after other non-queued requests
  431. * have been made of the scsi driver, to ensure that the queue
  432. * continues to be drained.
  433. *
  434. * must be called at the correct (highish) spl level
  435. * cdstart() is called at splbio from cdstrategy and scsi_done
  436. */
  437. void
  438. cdstart(struct scsi_xfer *xs)
  439. {
  440. struct scsi_link *sc_link = xs->sc_link;
  441. struct cd_softc *sc = sc_link->device_softc;
  442. struct buf *bp;
  443. struct scsi_rw_big *cmd_big;
  444. struct scsi_rw *cmd_small;
  445. u_int64_t secno, nsecs;
  446. struct partition *p;
  447. int read;
  448. SC_DEBUG(sc_link, SDEV_DB2, ("cdstart\n"));
  449. if (sc->sc_flags & CDF_DYING) {
  450. scsi_xs_put(xs);
  451. return;
  452. }
  453. /*
  454. * If the device has become invalid, abort all the
  455. * reads and writes until all files have been closed and
  456. * re-opened
  457. */
  458. if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
  459. bufq_drain(&sc->sc_bufq);
  460. scsi_xs_put(xs);
  461. return;
  462. }
  463. bp = bufq_dequeue(&sc->sc_bufq);
  464. if (bp == NULL) {
  465. scsi_xs_put(xs);
  466. return;
  467. }
  468. /*
  469. * We have a buf, now we should make a command
  470. *
  471. * First, translate the block to absolute and put it in terms
  472. * of the logical blocksize of the device.
  473. */
  474. secno = DL_BLKTOSEC(sc->sc_dk.dk_label, bp->b_blkno);
  475. p = &sc->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
  476. secno += DL_GETPOFFSET(p);
  477. nsecs = howmany(bp->b_bcount, sc->sc_dk.dk_label->d_secsize);
  478. read = (bp->b_flags & B_READ);
  479. /*
  480. * Fill out the scsi command. If the transfer will
  481. * fit in a "small" cdb, use it.
  482. */
  483. if (!(sc_link->flags & SDEV_ATAPI) &&
  484. !(sc_link->quirks & SDEV_ONLYBIG) &&
  485. ((secno & 0x1fffff) == secno) &&
  486. ((nsecs & 0xff) == nsecs)) {
  487. /*
  488. * We can fit in a small cdb.
  489. */
  490. cmd_small = (struct scsi_rw *)xs->cmd;
  491. cmd_small->opcode = read ?
  492. READ_COMMAND : WRITE_COMMAND;
  493. _lto3b(secno, cmd_small->addr);
  494. cmd_small->length = nsecs & 0xff;
  495. xs->cmdlen = sizeof(*cmd_small);
  496. } else {
  497. /*
  498. * Need a large cdb.
  499. */
  500. cmd_big = (struct scsi_rw_big *)xs->cmd;
  501. cmd_big->opcode = read ?
  502. READ_BIG : WRITE_BIG;
  503. _lto4b(secno, cmd_big->addr);
  504. _lto2b(nsecs, cmd_big->length);
  505. xs->cmdlen = sizeof(*cmd_big);
  506. }
  507. xs->flags |= (read ? SCSI_DATA_IN : SCSI_DATA_OUT);
  508. xs->timeout = 30000;
  509. xs->data = bp->b_data;
  510. xs->datalen = bp->b_bcount;
  511. xs->done = cd_buf_done;
  512. xs->cookie = bp;
  513. xs->bp = bp;
  514. /* Instrumentation. */
  515. disk_busy(&sc->sc_dk);
  516. scsi_xs_exec(xs);
  517. if (ISSET(sc->sc_flags, CDF_WAITING))
  518. CLR(sc->sc_flags, CDF_WAITING);
  519. else if (bufq_peek(&sc->sc_bufq))
  520. scsi_xsh_add(&sc->sc_xsh);
  521. }
  522. void
  523. cd_buf_done(struct scsi_xfer *xs)
  524. {
  525. struct cd_softc *sc = xs->sc_link->device_softc;
  526. struct buf *bp = xs->cookie;
  527. int error, s;
  528. switch (xs->error) {
  529. case XS_NOERROR:
  530. bp->b_error = 0;
  531. bp->b_resid = xs->resid;
  532. break;
  533. case XS_NO_CCB:
  534. /* The adapter is busy, requeue the buf and try it later. */
  535. disk_unbusy(&sc->sc_dk, bp->b_bcount - xs->resid,
  536. bp->b_flags & B_READ);
  537. bufq_requeue(&sc->sc_bufq, bp);
  538. scsi_xs_put(xs);
  539. SET(sc->sc_flags, CDF_WAITING);
  540. timeout_add(&sc->sc_timeout, 1);
  541. return;
  542. case XS_SENSE:
  543. case XS_SHORTSENSE:
  544. #ifdef SCSIDEBUG
  545. scsi_sense_print_debug(xs);
  546. #endif
  547. error = cd_interpret_sense(xs);
  548. if (error == 0) {
  549. bp->b_error = 0;
  550. bp->b_resid = xs->resid;
  551. break;
  552. }
  553. if (error != ERESTART)
  554. xs->retries = 0;
  555. goto retry;
  556. case XS_BUSY:
  557. if (xs->retries) {
  558. if (scsi_delay(xs, 1) != ERESTART)
  559. xs->retries = 0;
  560. }
  561. goto retry;
  562. case XS_TIMEOUT:
  563. retry:
  564. if (xs->retries--) {
  565. scsi_xs_exec(xs);
  566. return;
  567. }
  568. /* FALLTHROUGH */
  569. default:
  570. bp->b_error = EIO;
  571. bp->b_flags |= B_ERROR;
  572. bp->b_resid = bp->b_bcount;
  573. break;
  574. }
  575. disk_unbusy(&sc->sc_dk, bp->b_bcount - xs->resid,
  576. bp->b_flags & B_READ);
  577. s = splbio();
  578. biodone(bp);
  579. splx(s);
  580. scsi_xs_put(xs);
  581. }
  582. void
  583. cdminphys(struct buf *bp)
  584. {
  585. struct cd_softc *sc;
  586. long max;
  587. sc = cdlookup(DISKUNIT(bp->b_dev));
  588. if (sc == NULL)
  589. return;
  590. /*
  591. * If the device is ancient, we want to make sure that
  592. * the transfer fits into a 6-byte cdb.
  593. *
  594. * XXX Note that the SCSI-I spec says that 256-block transfers
  595. * are allowed in a 6-byte read/write, and are specified
  596. * by setting the "length" to 0. However, we're conservative
  597. * here, allowing only 255-block transfers in case an
  598. * ancient device gets confused by length == 0. A length of 0
  599. * in a 10-byte read/write actually means 0 blocks.
  600. */
  601. if (sc->sc_flags & CDF_ANCIENT) {
  602. max = sc->sc_dk.dk_label->d_secsize * 0xff;
  603. if (bp->b_bcount > max)
  604. bp->b_bcount = max;
  605. }
  606. (*sc->sc_link->adapter->scsi_minphys)(bp, sc->sc_link);
  607. device_unref(&sc->sc_dev);
  608. }
  609. int
  610. cdread(dev_t dev, struct uio *uio, int ioflag)
  611. {
  612. return (physio(cdstrategy, dev, B_READ, cdminphys, uio));
  613. }
  614. int
  615. cdwrite(dev_t dev, struct uio *uio, int ioflag)
  616. {
  617. return (physio(cdstrategy, dev, B_WRITE, cdminphys, uio));
  618. }
  619. /*
  620. * Perform special action on behalf of the user.
  621. * Knows about the internals of this device
  622. */
  623. int
  624. cdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
  625. {
  626. struct cd_softc *sc;
  627. struct disklabel *lp;
  628. int part = DISKPART(dev);
  629. int error = 0;
  630. sc = cdlookup(DISKUNIT(dev));
  631. if (sc == NULL)
  632. return ENXIO;
  633. if (sc->sc_flags & CDF_DYING) {
  634. device_unref(&sc->sc_dev);
  635. return (ENXIO);
  636. }
  637. SC_DEBUG(sc->sc_link, SDEV_DB2, ("cdioctl 0x%lx\n", cmd));
  638. /*
  639. * If the device is not valid.. abandon ship
  640. */
  641. if ((sc->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
  642. switch (cmd) {
  643. case DIOCLOCK:
  644. case DIOCEJECT:
  645. case SCIOCIDENTIFY:
  646. case SCIOCCOMMAND:
  647. case SCIOCDEBUG:
  648. case CDIOCLOADUNLOAD:
  649. case SCIOCRESET:
  650. case CDIOCGETVOL:
  651. case CDIOCSETVOL:
  652. case CDIOCSETMONO:
  653. case CDIOCSETSTEREO:
  654. case CDIOCSETMUTE:
  655. case CDIOCSETLEFT:
  656. case CDIOCSETRIGHT:
  657. case CDIOCCLOSE:
  658. case CDIOCEJECT:
  659. case CDIOCALLOW:
  660. case CDIOCPREVENT:
  661. case CDIOCSETDEBUG:
  662. case CDIOCCLRDEBUG:
  663. case CDIOCRESET:
  664. case DVD_AUTH:
  665. case DVD_READ_STRUCT:
  666. case MTIOCTOP:
  667. if (part == RAW_PART)
  668. break;
  669. /* FALLTHROUGH */
  670. default:
  671. if ((sc->sc_link->flags & SDEV_OPEN) == 0)
  672. error = ENODEV;
  673. else
  674. error = EIO;
  675. goto exit;
  676. }
  677. }
  678. switch (cmd) {
  679. case DIOCRLDINFO:
  680. lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK);
  681. cdgetdisklabel(dev, sc, lp, 0);
  682. memcpy(sc->sc_dk.dk_label, lp, sizeof(*lp));
  683. free(lp, M_TEMP, sizeof(*lp));
  684. break;
  685. case DIOCGPDINFO:
  686. cdgetdisklabel(dev, sc, (struct disklabel *)addr, 1);
  687. break;
  688. case DIOCGDINFO:
  689. *(struct disklabel *)addr = *(sc->sc_dk.dk_label);
  690. break;
  691. case DIOCGPART:
  692. ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
  693. ((struct partinfo *)addr)->part =
  694. &sc->sc_dk.dk_label->d_partitions[DISKPART(dev)];
  695. break;
  696. case DIOCWDINFO:
  697. case DIOCSDINFO:
  698. if ((flag & FWRITE) == 0) {
  699. error = EBADF;
  700. break;
  701. }
  702. if ((error = disk_lock(&sc->sc_dk)) != 0)
  703. break;
  704. error = setdisklabel(sc->sc_dk.dk_label,
  705. (struct disklabel *)addr, sc->sc_dk.dk_openmask);
  706. if (error == 0) {
  707. }
  708. disk_unlock(&sc->sc_dk);
  709. break;
  710. case CDIOCPLAYTRACKS: {
  711. struct ioc_play_track *args = (struct ioc_play_track *)addr;
  712. if ((error = cd_set_pa_immed(sc, 0)) != 0)
  713. break;
  714. error = cd_play_tracks(sc, args->start_track,
  715. args->start_index, args->end_track, args->end_index);
  716. break;
  717. }
  718. case CDIOCPLAYMSF: {
  719. struct ioc_play_msf *args = (struct ioc_play_msf *)addr;
  720. if ((error = cd_set_pa_immed(sc, 0)) != 0)
  721. break;
  722. error = cd_play_msf(sc, args->start_m, args->start_s,
  723. args->start_f, args->end_m, args->end_s, args->end_f);
  724. break;
  725. }
  726. case CDIOCPLAYBLOCKS: {
  727. struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr;
  728. if ((error = cd_set_pa_immed(sc, 0)) != 0)
  729. break;
  730. error = cd_play(sc, args->blk, args->len);
  731. break;
  732. }
  733. case CDIOCREADSUBCHANNEL: {
  734. struct ioc_read_subchannel *args =
  735. (struct ioc_read_subchannel *)addr;
  736. struct cd_sub_channel_info *data;
  737. int len = args->data_len;
  738. if (len > sizeof(*data) ||
  739. len < sizeof(struct cd_sub_channel_header)) {
  740. error = EINVAL;
  741. break;
  742. }
  743. data = dma_alloc(sizeof(*data), PR_WAITOK);
  744. error = cd_read_subchannel(sc, args->address_format,
  745. args->data_format, args->track, data, len);
  746. if (error) {
  747. dma_free(data, sizeof(*data));
  748. break;
  749. }
  750. len = min(len, _2btol(data->header.data_len) +
  751. sizeof(struct cd_sub_channel_header));
  752. error = copyout(data, args->data, len);
  753. dma_free(data, sizeof(*data));
  754. break;
  755. }
  756. case CDIOREADTOCHEADER: {
  757. struct ioc_toc_header *th;
  758. th = dma_alloc(sizeof(*th), PR_WAITOK);
  759. if ((error = cd_read_toc(sc, 0, 0, th, sizeof(*th), 0)) != 0) {
  760. dma_free(th, sizeof(*th));
  761. break;
  762. }
  763. if (sc->sc_link->quirks & ADEV_LITTLETOC)
  764. th->len = letoh16(th->len);
  765. else
  766. th->len = betoh16(th->len);
  767. if (th->len > 0)
  768. memcpy(addr, th, sizeof(*th));
  769. else
  770. error = EIO;
  771. dma_free(th, sizeof(*th));
  772. break;
  773. }
  774. case CDIOREADTOCENTRYS: {
  775. struct cd_toc *toc;
  776. struct ioc_read_toc_entry *te =
  777. (struct ioc_read_toc_entry *)addr;
  778. struct ioc_toc_header *th;
  779. struct cd_toc_entry *cte;
  780. int len = te->data_len;
  781. int ntracks;
  782. toc = dma_alloc(sizeof(*toc), PR_WAITOK | PR_ZERO);
  783. th = &toc->header;
  784. if (len > sizeof(toc->entries) ||
  785. len < sizeof(struct cd_toc_entry)) {
  786. dma_free(toc, sizeof(*toc));
  787. error = EINVAL;
  788. break;
  789. }
  790. error = cd_read_toc(sc, te->address_format, te->starting_track,
  791. toc, len + sizeof(struct ioc_toc_header), 0);
  792. if (error) {
  793. dma_free(toc, sizeof(*toc));
  794. break;
  795. }
  796. if (te->address_format == CD_LBA_FORMAT)
  797. for (ntracks =
  798. th->ending_track - th->starting_track + 1;
  799. ntracks >= 0; ntracks--) {
  800. cte = &toc->entries[ntracks];
  801. cte->addr_type = CD_LBA_FORMAT;
  802. if (sc->sc_link->quirks & ADEV_LITTLETOC) {
  803. #if BYTE_ORDER == BIG_ENDIAN
  804. swap16_multi((u_int16_t *)&cte->addr,
  805. sizeof(cte->addr) / 2);
  806. #endif
  807. } else
  808. cte->addr.lba = betoh32(cte->addr.lba);
  809. }
  810. if (sc->sc_link->quirks & ADEV_LITTLETOC) {
  811. th->len = letoh16(th->len);
  812. } else
  813. th->len = betoh16(th->len);
  814. len = min(len, th->len - (sizeof(th->starting_track) +
  815. sizeof(th->ending_track)));
  816. error = copyout(toc->entries, te->data, len);
  817. dma_free(toc, sizeof(*toc));
  818. break;
  819. }
  820. case CDIOREADMSADDR: {
  821. struct cd_toc *toc;
  822. int sessno = *(int *)addr;
  823. struct cd_toc_entry *cte;
  824. if (sessno != 0) {
  825. error = EINVAL;
  826. break;
  827. }
  828. toc = dma_alloc(sizeof(*toc), PR_WAITOK | PR_ZERO);
  829. error = cd_read_toc(sc, 0, 0, toc,
  830. sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry),
  831. 0x40 /* control word for "get MS info" */);
  832. if (error) {
  833. dma_free(toc, sizeof(*toc));
  834. break;
  835. }
  836. cte = &toc->entries[0];
  837. if (sc->sc_link->quirks & ADEV_LITTLETOC) {
  838. #if BYTE_ORDER == BIG_ENDIAN
  839. swap16_multi((u_int16_t *)&cte->addr,
  840. sizeof(cte->addr) / 2);
  841. #endif
  842. } else
  843. cte->addr.lba = betoh32(cte->addr.lba);
  844. if (sc->sc_link->quirks & ADEV_LITTLETOC)
  845. toc->header.len = letoh16(toc->header.len);
  846. else
  847. toc->header.len = betoh16(toc->header.len);
  848. *(int *)addr = (toc->header.len >= 10 && cte->track > 1) ?
  849. cte->addr.lba : 0;
  850. dma_free(toc, sizeof(*toc));
  851. break;
  852. }
  853. case CDIOCSETPATCH: {
  854. struct ioc_patch *arg = (struct ioc_patch *)addr;
  855. error = cd_setchan(sc, arg->patch[0], arg->patch[1],
  856. arg->patch[2], arg->patch[3], 0);
  857. break;
  858. }
  859. case CDIOCGETVOL: {
  860. struct ioc_vol *arg = (struct ioc_vol *)addr;
  861. error = cd_getvol(sc, arg, 0);
  862. break;
  863. }
  864. case CDIOCSETVOL: {
  865. struct ioc_vol *arg = (struct ioc_vol *)addr;
  866. error = cd_setvol(sc, arg, 0);
  867. break;
  868. }
  869. case CDIOCSETMONO:
  870. error = cd_setchan(sc, BOTH_CHANNEL, BOTH_CHANNEL, MUTE_CHANNEL,
  871. MUTE_CHANNEL, 0);
  872. break;
  873. case CDIOCSETSTEREO:
  874. error = cd_setchan(sc, LEFT_CHANNEL, RIGHT_CHANNEL,
  875. MUTE_CHANNEL, MUTE_CHANNEL, 0);
  876. break;
  877. case CDIOCSETMUTE:
  878. error = cd_setchan(sc, MUTE_CHANNEL, MUTE_CHANNEL, MUTE_CHANNEL,
  879. MUTE_CHANNEL, 0);
  880. break;
  881. case CDIOCSETLEFT:
  882. error = cd_setchan(sc, LEFT_CHANNEL, LEFT_CHANNEL, MUTE_CHANNEL,
  883. MUTE_CHANNEL, 0);
  884. break;
  885. case CDIOCSETRIGHT:
  886. error = cd_setchan(sc, RIGHT_CHANNEL, RIGHT_CHANNEL,
  887. MUTE_CHANNEL, MUTE_CHANNEL, 0);
  888. break;
  889. case CDIOCRESUME:
  890. error = cd_pause(sc, 1);
  891. break;
  892. case CDIOCPAUSE:
  893. error = cd_pause(sc, 0);
  894. break;
  895. case CDIOCSTART:
  896. error = scsi_start(sc->sc_link, SSS_START, 0);
  897. break;
  898. case CDIOCSTOP:
  899. error = scsi_start(sc->sc_link, SSS_STOP, 0);
  900. break;
  901. close_tray:
  902. case CDIOCCLOSE:
  903. error = scsi_start(sc->sc_link, SSS_START|SSS_LOEJ,
  904. SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE);
  905. break;
  906. case MTIOCTOP:
  907. if (((struct mtop *)addr)->mt_op == MTRETEN)
  908. goto close_tray;
  909. if (((struct mtop *)addr)->mt_op != MTOFFL) {
  910. error = EIO;
  911. break;
  912. }
  913. /* FALLTHROUGH */
  914. case CDIOCEJECT: /* FALLTHROUGH */
  915. case DIOCEJECT:
  916. sc->sc_link->flags |= SDEV_EJECTING;
  917. break;
  918. case CDIOCALLOW:
  919. error = scsi_prevent(sc->sc_link, PR_ALLOW, 0);
  920. break;
  921. case CDIOCPREVENT:
  922. error = scsi_prevent(sc->sc_link, PR_PREVENT, 0);
  923. break;
  924. case DIOCLOCK:
  925. error = scsi_prevent(sc->sc_link,
  926. (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0);
  927. break;
  928. case CDIOCSETDEBUG:
  929. sc->sc_link->flags |= (SDEV_DB1 | SDEV_DB2);
  930. break;
  931. case CDIOCCLRDEBUG:
  932. sc->sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2);
  933. break;
  934. case CDIOCRESET:
  935. case SCIOCRESET:
  936. error = cd_reset(sc);
  937. break;
  938. case CDIOCLOADUNLOAD: {
  939. struct ioc_load_unload *args = (struct ioc_load_unload *)addr;
  940. error = cd_load_unload(sc, args->options, args->slot);
  941. break;
  942. }
  943. case DVD_AUTH:
  944. error = dvd_auth(sc, (union dvd_authinfo *)addr);
  945. break;
  946. case DVD_READ_STRUCT:
  947. error = dvd_read_struct(sc, (union dvd_struct *)addr);
  948. break;
  949. default:
  950. if (DISKPART(dev) != RAW_PART) {
  951. error = ENOTTY;
  952. break;
  953. }
  954. error = scsi_do_ioctl(sc->sc_link, cmd, addr, flag);
  955. break;
  956. }
  957. exit:
  958. device_unref(&sc->sc_dev);
  959. return (error);
  960. }
  961. /*
  962. * Load the label information on the named device
  963. * Actually fabricate a disklabel
  964. *
  965. * EVENTUALLY take information about different
  966. * data tracks from the TOC and put it in the disklabel
  967. */
  968. int
  969. cdgetdisklabel(dev_t dev, struct cd_softc *sc, struct disklabel *lp,
  970. int spoofonly)
  971. {
  972. struct cd_toc *toc;
  973. int tocidx, n, audioonly = 1;
  974. bzero(lp, sizeof(struct disklabel));
  975. lp->d_secsize = sc->params.secsize;
  976. lp->d_ntracks = 1;
  977. lp->d_nsectors = 100;
  978. lp->d_secpercyl = 100;
  979. lp->d_ncylinders = (sc->params.disksize / 100) + 1;
  980. if (sc->sc_link->flags & SDEV_ATAPI) {
  981. strncpy(lp->d_typename, "ATAPI CD-ROM", sizeof(lp->d_typename));
  982. lp->d_type = DTYPE_ATAPI;
  983. } else {
  984. strncpy(lp->d_typename, "SCSI CD-ROM", sizeof(lp->d_typename));
  985. lp->d_type = DTYPE_SCSI;
  986. }
  987. strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
  988. DL_SETDSIZE(lp, sc->params.disksize);
  989. lp->d_version = 1;
  990. /* XXX - these values for BBSIZE and SBSIZE assume ffs */
  991. lp->d_bbsize = BBSIZE;
  992. lp->d_sbsize = SBSIZE;
  993. lp->d_magic = DISKMAGIC;
  994. lp->d_magic2 = DISKMAGIC;
  995. lp->d_checksum = dkcksum(lp);
  996. toc = dma_alloc(sizeof(*toc), PR_WAITOK | PR_ZERO);
  997. if (cd_load_toc(sc, toc, CD_LBA_FORMAT)) {
  998. audioonly = 0; /* No valid TOC found == not an audio CD. */
  999. goto done;
  1000. }
  1001. n = toc->header.ending_track - toc->header.starting_track + 1;
  1002. for (tocidx = 0; tocidx < n; tocidx++)
  1003. if (toc->entries[tocidx].control & 4) {
  1004. audioonly = 0; /* Found a non-audio track. */
  1005. goto done;
  1006. }
  1007. done:
  1008. dma_free(toc, sizeof(*toc));
  1009. if (audioonly)
  1010. return (0);
  1011. return readdisklabel(DISKLABELDEV(dev), cdstrategy, lp, spoofonly);
  1012. }
  1013. int
  1014. cd_setchan(struct cd_softc *sc, int p0, int p1, int p2, int p3, int flags)
  1015. {
  1016. union scsi_mode_sense_buf *data;
  1017. struct cd_audio_page *audio = NULL;
  1018. int error, big;
  1019. data = dma_alloc(sizeof(*data), PR_NOWAIT);
  1020. if (data == NULL)
  1021. return (ENOMEM);
  1022. error = scsi_do_mode_sense(sc->sc_link, AUDIO_PAGE, data,
  1023. (void **)&audio, NULL, NULL, NULL, sizeof(*audio), flags, &big);
  1024. if (error == 0 && audio == NULL)
  1025. error = EIO;
  1026. if (error == 0) {
  1027. audio->port[LEFT_PORT].channels = p0;
  1028. audio->port[RIGHT_PORT].channels = p1;
  1029. audio->port[2].channels = p2;
  1030. audio->port[3].channels = p3;
  1031. if (big)
  1032. error = scsi_mode_select_big(sc->sc_link, SMS_PF,
  1033. &data->hdr_big, flags, 20000);
  1034. else
  1035. error = scsi_mode_select(sc->sc_link, SMS_PF,
  1036. &data->hdr, flags, 20000);
  1037. }
  1038. dma_free(data, sizeof(*data));
  1039. return (error);
  1040. }
  1041. int
  1042. cd_getvol(struct cd_softc *sc, struct ioc_vol *arg, int flags)
  1043. {
  1044. union scsi_mode_sense_buf *data;
  1045. struct cd_audio_page *audio = NULL;
  1046. int error;
  1047. data = dma_alloc(sizeof(*data), PR_NOWAIT);
  1048. if (data == NULL)
  1049. return (ENOMEM);
  1050. error = scsi_do_mode_sense(sc->sc_link, AUDIO_PAGE, data,
  1051. (void **)&audio, NULL, NULL, NULL, sizeof(*audio), flags, NULL);
  1052. if (error == 0 && audio == NULL)
  1053. error = EIO;
  1054. if (error == 0) {
  1055. arg->vol[0] = audio->port[0].volume;
  1056. arg->vol[1] = audio->port[1].volume;
  1057. arg->vol[2] = audio->port[2].volume;
  1058. arg->vol[3] = audio->port[3].volume;
  1059. }
  1060. dma_free(data, sizeof(*data));
  1061. return (0);
  1062. }
  1063. int
  1064. cd_setvol(struct cd_softc *sc, const struct ioc_vol *arg, int flags)
  1065. {
  1066. union scsi_mode_sense_buf *data;
  1067. struct cd_audio_page *audio = NULL;
  1068. u_int8_t mask_volume[4];
  1069. int error, big;
  1070. data = dma_alloc(sizeof(*data), PR_NOWAIT);
  1071. if (data == NULL)
  1072. return (ENOMEM);
  1073. error = scsi_do_mode_sense(sc->sc_link,
  1074. AUDIO_PAGE | SMS_PAGE_CTRL_CHANGEABLE, data, (void **)&audio, NULL,
  1075. NULL, NULL, sizeof(*audio), flags, NULL);
  1076. if (error == 0 && audio == NULL)
  1077. error = EIO;
  1078. if (error != 0) {
  1079. dma_free(data, sizeof(*data));
  1080. return (error);
  1081. }
  1082. mask_volume[0] = audio->port[0].volume;
  1083. mask_volume[1] = audio->port[1].volume;
  1084. mask_volume[2] = audio->port[2].volume;
  1085. mask_volume[3] = audio->port[3].volume;
  1086. error = scsi_do_mode_sense(sc->sc_link, AUDIO_PAGE, data,
  1087. (void **)&audio, NULL, NULL, NULL, sizeof(*audio), flags, &big);
  1088. if (error == 0 && audio == NULL)
  1089. error = EIO;
  1090. if (error != 0) {
  1091. dma_free(data, sizeof(*data));
  1092. return (error);
  1093. }
  1094. audio->port[0].volume = arg->vol[0] & mask_volume[0];
  1095. audio->port[1].volume = arg->vol[1] & mask_volume[1];
  1096. audio->port[2].volume = arg->vol[2] & mask_volume[2];
  1097. audio->port[3].volume = arg->vol[3] & mask_volume[3];
  1098. if (big)
  1099. error = scsi_mode_select_big(sc->sc_link, SMS_PF,
  1100. &data->hdr_big, flags, 20000);
  1101. else
  1102. error = scsi_mode_select(sc->sc_link, SMS_PF,
  1103. &data->hdr, flags, 20000);
  1104. dma_free(data, sizeof(*data));
  1105. return (error);
  1106. }
  1107. int
  1108. cd_load_unload(struct cd_softc *sc, int options, int slot)
  1109. {
  1110. struct scsi_load_unload *cmd;
  1111. struct scsi_xfer *xs;
  1112. int error;
  1113. xs = scsi_xs_get(sc->sc_link, 0);
  1114. if (xs == NULL)
  1115. return (ENOMEM);
  1116. xs->cmdlen = sizeof(*cmd);
  1117. xs->timeout = 200000;
  1118. cmd = (struct scsi_load_unload *)xs->cmd;
  1119. cmd->opcode = LOAD_UNLOAD;
  1120. cmd->options = options; /* ioctl uses ATAPI values */
  1121. cmd->slot = slot;
  1122. error = scsi_xs_sync(xs);
  1123. scsi_xs_put(xs);
  1124. return (error);
  1125. }
  1126. int
  1127. cd_set_pa_immed(struct cd_softc *sc, int flags)
  1128. {
  1129. union scsi_mode_sense_buf *data;
  1130. struct cd_audio_page *audio = NULL;
  1131. int error, oflags, big;
  1132. if (sc->sc_link->flags & SDEV_ATAPI)
  1133. /* XXX Noop? */
  1134. return (0);
  1135. data = dma_alloc(sizeof(*data), PR_NOWAIT);
  1136. if (data == NULL)
  1137. return (ENOMEM);
  1138. error = scsi_do_mode_sense(sc->sc_link, AUDIO_PAGE, data,
  1139. (void **)&audio, NULL, NULL, NULL, sizeof(*audio), flags, &big);
  1140. if (error == 0 && audio == NULL)
  1141. error = EIO;
  1142. if (error == 0) {
  1143. oflags = audio->flags;
  1144. audio->flags &= ~CD_PA_SOTC;
  1145. audio->flags |= CD_PA_IMMED;
  1146. if (audio->flags != oflags) {
  1147. if (big)
  1148. error = scsi_mode_select_big(sc->sc_link,
  1149. SMS_PF, &data->hdr_big, flags, 20000);
  1150. else
  1151. error = scsi_mode_select(sc->sc_link, SMS_PF,
  1152. &data->hdr, flags, 20000);
  1153. }
  1154. }
  1155. dma_free(data, sizeof(*data));
  1156. return (error);
  1157. }
  1158. /*
  1159. * Get scsi driver to send a "start playing" command
  1160. */
  1161. int
  1162. cd_play(struct cd_softc *sc, int secno, int nsecs)
  1163. {
  1164. struct scsi_play *cmd;
  1165. struct scsi_xfer *xs;
  1166. int error;
  1167. xs = scsi_xs_get(sc->sc_link, 0);
  1168. if (xs == NULL)
  1169. return (ENOMEM);
  1170. xs->cmdlen = sizeof(*cmd);
  1171. xs->timeout = 200000;
  1172. cmd = (struct scsi_play *)xs->cmd;
  1173. cmd->opcode = PLAY;
  1174. _lto4b(secno, cmd->blk_addr);
  1175. _lto2b(nsecs, cmd->xfer_len);
  1176. error = scsi_xs_sync(xs);
  1177. scsi_xs_put(xs);
  1178. return (error);
  1179. }
  1180. /*
  1181. * Get scsi driver to send a "start playing" command
  1182. */
  1183. int
  1184. cd_play_tracks(struct cd_softc *sc, int strack, int sindex, int etrack,
  1185. int eindex)
  1186. {
  1187. struct cd_toc *toc;
  1188. u_char endf, ends, endm;
  1189. int error;
  1190. if (!etrack)
  1191. return (EIO);
  1192. if (strack > etrack)
  1193. return (EINVAL);
  1194. toc = dma_alloc(sizeof(*toc), PR_WAITOK | PR_ZERO);
  1195. if ((error = cd_load_toc(sc, toc, CD_MSF_FORMAT)) != 0)
  1196. goto done;
  1197. if (++etrack > (toc->header.ending_track+1))
  1198. etrack = toc->header.ending_track+1;
  1199. strack -= toc->header.starting_track;
  1200. etrack -= toc->header.starting_track;
  1201. if (strack < 0) {
  1202. error = EINVAL;
  1203. goto done;
  1204. }
  1205. /*
  1206. * The track ends one frame before the next begins. The last track
  1207. * is taken care of by the leadoff track.
  1208. */
  1209. endm = toc->entries[etrack].addr.msf.minute;
  1210. ends = toc->entries[etrack].addr.msf.second;
  1211. endf = toc->entries[etrack].addr.msf.frame;
  1212. if (endf-- == 0) {
  1213. endf = CD_FRAMES - 1;
  1214. if (ends-- == 0) {
  1215. ends = CD_SECS - 1;
  1216. if (endm-- == 0) {
  1217. error = EINVAL;
  1218. goto done;
  1219. }
  1220. }
  1221. }
  1222. error = cd_play_msf(sc, toc->entries[strack].addr.msf.minute,
  1223. toc->entries[strack].addr.msf.second,
  1224. toc->entries[strack].addr.msf.frame,
  1225. endm, ends, endf);
  1226. done:
  1227. dma_free(toc, sizeof(*toc));
  1228. return (error);
  1229. }
  1230. /*
  1231. * Get scsi driver to send a "play msf" command
  1232. */
  1233. int
  1234. cd_play_msf(struct cd_softc *sc, int startm, int starts, int startf, int endm,
  1235. int ends, int endf)
  1236. {
  1237. struct scsi_play_msf *cmd;
  1238. struct scsi_xfer *xs;
  1239. int error;
  1240. xs = scsi_xs_get(sc->sc_link, 0);
  1241. if (xs == NULL)
  1242. return (ENOMEM);
  1243. xs->cmdlen = sizeof(*cmd);
  1244. xs->timeout = 20000;
  1245. cmd = (struct scsi_play_msf *)xs->cmd;
  1246. cmd->opcode = PLAY_MSF;
  1247. cmd->start_m = startm;
  1248. cmd->start_s = starts;
  1249. cmd->start_f = startf;
  1250. cmd->end_m = endm;
  1251. cmd->end_s = ends;
  1252. cmd->end_f = endf;
  1253. error = scsi_xs_sync(xs);
  1254. scsi_xs_put(xs);
  1255. return (error);
  1256. }
  1257. /*
  1258. * Get scsi driver to send a "start up" command
  1259. */
  1260. int
  1261. cd_pause(struct cd_softc *sc, int go)
  1262. {
  1263. struct scsi_pause *cmd;
  1264. struct scsi_xfer *xs;
  1265. int error;
  1266. xs = scsi_xs_get(sc->sc_link, 0);
  1267. if (xs == NULL)
  1268. return (ENOMEM);
  1269. xs->cmdlen = sizeof(*cmd);
  1270. xs->timeout = 2000;
  1271. cmd = (struct scsi_pause *)xs->cmd;
  1272. cmd->opcode = PAUSE;
  1273. cmd->resume = go;
  1274. error = scsi_xs_sync(xs);
  1275. scsi_xs_put(xs);
  1276. return (error);
  1277. }
  1278. /*
  1279. * Get scsi driver to send a "RESET" command
  1280. */
  1281. int
  1282. cd_reset(struct cd_softc *sc)
  1283. {
  1284. struct scsi_xfer *xs;
  1285. int error;
  1286. xs = scsi_xs_get(sc->sc_link, SCSI_RESET);
  1287. if (xs == NULL)
  1288. return (ENOMEM);
  1289. xs->timeout = 2000;
  1290. error = scsi_xs_sync(xs);
  1291. scsi_xs_put(xs);
  1292. return (error);
  1293. }
  1294. /*
  1295. * Read subchannel
  1296. */
  1297. int
  1298. cd_read_subchannel(struct cd_softc *sc, int mode, int format, int track,
  1299. struct cd_sub_channel_info *data, int len)
  1300. {
  1301. struct scsi_read_subchannel *cmd;
  1302. struct scsi_xfer *xs;
  1303. int error;
  1304. xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN | SCSI_SILENT);
  1305. if (xs == NULL)
  1306. return (ENOMEM);
  1307. xs->cmdlen = sizeof(*cmd);
  1308. xs->data = (void *)data;
  1309. xs->datalen = len;
  1310. xs->timeout = 5000;
  1311. cmd = (struct scsi_read_subchannel *)xs->cmd;
  1312. cmd->opcode = READ_SUBCHANNEL;
  1313. if (mode == CD_MSF_FORMAT)
  1314. cmd->byte2 |= CD_MSF;
  1315. cmd->byte3 = SRS_SUBQ;
  1316. cmd->subchan_format = format;
  1317. cmd->track = track;
  1318. _lto2b(len, cmd->data_len);
  1319. error = scsi_xs_sync(xs);
  1320. scsi_xs_put(xs);
  1321. return (error);
  1322. }
  1323. /*
  1324. * Read table of contents
  1325. */
  1326. int
  1327. cd_read_toc(struct cd_softc *sc, int mode, int start, void *data, int len,
  1328. int control)
  1329. {
  1330. struct scsi_read_toc *cmd;
  1331. struct scsi_xfer *xs;
  1332. int error;
  1333. xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN |
  1334. SCSI_IGNORE_ILLEGAL_REQUEST);
  1335. if (xs == NULL)
  1336. return (ENOMEM);
  1337. xs->cmdlen = sizeof(*cmd);
  1338. xs->data = data;
  1339. xs->datalen = len;
  1340. xs->timeout = 5000;
  1341. bzero(data, len);
  1342. cmd = (struct scsi_read_toc *)xs->cmd;
  1343. cmd->opcode = READ_TOC;
  1344. if (mode == CD_MSF_FORMAT)
  1345. cmd->byte2 |= CD_MSF;
  1346. cmd->from_track = start;
  1347. _lto2b(len, cmd->data_len);
  1348. cmd->control = control;
  1349. error = scsi_xs_sync(xs);
  1350. scsi_xs_put(xs);
  1351. return (error);
  1352. }
  1353. int
  1354. cd_load_toc(struct cd_softc *sc, struct cd_toc *toc, int fmt)
  1355. {
  1356. int n, len, error;
  1357. error = cd_read_toc(sc, 0, 0, toc, sizeof(toc->header), 0);
  1358. if (error == 0) {
  1359. if (toc->header.ending_track < toc->header.starting_track)
  1360. return (EIO);
  1361. /* +2 to account for leading out track. */
  1362. n = toc->header.ending_track - toc->header.starting_track + 2;
  1363. len = n * sizeof(struct cd_toc_entry) + sizeof(toc->header);
  1364. error = cd_read_toc(sc, fmt, 0, toc, len, 0);
  1365. }
  1366. return (error);
  1367. }
  1368. /*
  1369. * Get the scsi driver to send a full inquiry to the device and use the
  1370. * results to fill out the disk parameter structure.
  1371. */
  1372. int
  1373. cd_get_parms(struct cd_softc *sc, int flags)
  1374. {
  1375. /* Reasonable defaults for drives that don't support READ_CAPACITY */
  1376. sc->params.secsize = 2048;
  1377. sc->params.disksize = 400000;
  1378. if (sc->sc_link->quirks & ADEV_NOCAPACITY)
  1379. return (0);
  1380. sc->params.disksize = cd_size(sc->sc_link, flags, &sc->params.secsize);
  1381. if ((sc->params.secsize < 512) ||
  1382. ((sc->params.secsize & 511) != 0))
  1383. sc->params.secsize = 2048; /* some drives lie ! */
  1384. if (sc->params.disksize < 100)
  1385. sc->params.disksize = 400000;
  1386. return (0);
  1387. }
  1388. daddr_t
  1389. cdsize(dev_t dev)
  1390. {
  1391. /* CD-ROMs are read-only. */
  1392. return -1;
  1393. }
  1394. int
  1395. cddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
  1396. {
  1397. /* Not implemented. */
  1398. return ENXIO;
  1399. }
  1400. #define dvd_copy_key(dst, src) memcpy((dst), (src), DVD_KEY_SIZE)
  1401. #define dvd_copy_challenge(dst, src) memcpy((dst), (src), DVD_CHALLENGE_SIZE)
  1402. #define DVD_AUTH_BUFSIZE 20
  1403. int
  1404. dvd_auth(struct cd_softc *sc, union dvd_authinfo *a)
  1405. {
  1406. struct scsi_generic *cmd;
  1407. struct scsi_xfer *xs;
  1408. u_int8_t *buf;
  1409. int error;
  1410. buf = dma_alloc(DVD_AUTH_BUFSIZE, PR_WAITOK | PR_ZERO);
  1411. if (buf == NULL)
  1412. return (ENOMEM);
  1413. xs = scsi_xs_get(sc->sc_link, 0);
  1414. if (xs == NULL) {
  1415. error = ENOMEM;
  1416. goto done;
  1417. }
  1418. xs->cmdlen = sizeof(*cmd);
  1419. xs->timeout = 30000;
  1420. xs->data = buf;
  1421. cmd = xs->cmd;
  1422. switch (a->type) {
  1423. case DVD_LU_SEND_AGID:
  1424. cmd->opcode = GPCMD_REPORT_KEY;
  1425. cmd->bytes[8] = 8;
  1426. cmd->bytes[9] = 0 | (0 << 6);
  1427. xs->datalen = 8;
  1428. xs->flags |= SCSI_DATA_IN;
  1429. error = scsi_xs_sync(xs);
  1430. scsi_xs_put(xs);
  1431. if (error == 0)
  1432. a->lsa.agid = buf[7] >> 6;
  1433. break;
  1434. case DVD_LU_SEND_CHALLENGE:
  1435. cmd->opcode = GPCMD_REPORT_KEY;
  1436. cmd->bytes[8] = 16;
  1437. cmd->bytes[9] = 1 | (a->lsc.agid << 6);
  1438. xs->datalen = 16;
  1439. xs->flags |= SCSI_DATA_IN;
  1440. error = scsi_xs_sync(xs);
  1441. scsi_xs_put(xs);
  1442. if (error == 0)
  1443. dvd_copy_challenge(a->lsc.chal, &buf[4]);
  1444. break;
  1445. case DVD_LU_SEND_KEY1:
  1446. cmd->opcode = GPCMD_REPORT_KEY;
  1447. cmd->bytes[8] = 12;
  1448. cmd->bytes[9] = 2 | (a->lsk.agid << 6);
  1449. xs->datalen = 12;
  1450. xs->flags |= SCSI_DATA_IN;
  1451. error = scsi_xs_sync(xs);
  1452. scsi_xs_put(xs);
  1453. if (error == 0)
  1454. dvd_copy_key(a->lsk.key, &buf[4]);
  1455. break;
  1456. case DVD_LU_SEND_TITLE_KEY:
  1457. cmd->opcode = GPCMD_REPORT_KEY;
  1458. _lto4b(a->lstk.lba, &cmd->bytes[1]);
  1459. cmd->bytes[8] = 12;
  1460. cmd->bytes[9] = 4 | (a->lstk.agid << 6);
  1461. xs->datalen = 12;
  1462. xs->flags |= SCSI_DATA_IN;
  1463. error = scsi_xs_sync(xs);
  1464. scsi_xs_put(xs);
  1465. if (error == 0) {
  1466. a->lstk.cpm = (buf[4] >> 7) & 1;
  1467. a->lstk.cp_sec = (buf[4] >> 6) & 1;
  1468. a->lstk.cgms = (buf[4] >> 4) & 3;
  1469. dvd_copy_key(a->lstk.title_key, &buf[5]);
  1470. }
  1471. break;
  1472. case DVD_LU_SEND_ASF:
  1473. cmd->opcode = GPCMD_REPORT_KEY;
  1474. cmd->bytes[8] = 8;
  1475. cmd->bytes[9] = 5 | (a->lsasf.agid << 6);
  1476. xs->datalen = 8;
  1477. xs->flags |= SCSI_DATA_IN;
  1478. error = scsi_xs_sync(xs);
  1479. scsi_xs_put(xs);
  1480. if (error == 0)
  1481. a->lsasf.asf = buf[7] & 1;
  1482. break;
  1483. case DVD_HOST_SEND_CHALLENGE:
  1484. cmd->opcode = GPCMD_SEND_KEY;
  1485. cmd->bytes[8] = 16;
  1486. cmd->bytes[9] = 1 | (a->hsc.agid << 6);
  1487. buf[1] = 14;
  1488. dvd_copy_challenge(&buf[4], a->hsc.chal);
  1489. xs->datalen = 16;
  1490. xs->flags |= SCSI_DATA_OUT;
  1491. error = scsi_xs_sync(xs);
  1492. scsi_xs_put(xs);
  1493. if (error == 0)
  1494. a->type = DVD_LU_SEND_KEY1;
  1495. break;
  1496. case DVD_HOST_SEND_KEY2:
  1497. cmd->opcode = GPCMD_SEND_KEY;
  1498. cmd->bytes[8] = 12;
  1499. cmd->bytes[9] = 3 | (a->hsk.agid << 6);
  1500. buf[1] = 10;
  1501. dvd_copy_key(&buf[4], a->hsk.key);
  1502. xs->datalen = 12;
  1503. xs->flags |= SCSI_DATA_OUT;
  1504. error = scsi_xs_sync(xs);
  1505. scsi_xs_put(xs);
  1506. if (error == 0)
  1507. a->type = DVD_AUTH_ESTABLISHED;
  1508. else
  1509. a->type = DVD_AUTH_FAILURE;
  1510. break;
  1511. case DVD_INVALIDATE_AGID:
  1512. cmd->opcode = GPCMD_REPORT_KEY;
  1513. cmd->bytes[9] = 0x3f | (a->lsa.agid << 6);
  1514. xs->data = NULL;
  1515. error = scsi_xs_sync(xs);
  1516. scsi_xs_put(xs);
  1517. break;
  1518. case DVD_LU_SEND_RPC_STATE:
  1519. cmd->opcode = GPCMD_REPORT_KEY;
  1520. cmd->bytes[8] = 8;
  1521. cmd->bytes[9] = 8 | (0 << 6);
  1522. xs->datalen = 8;
  1523. xs->flags |= SCSI_DATA_IN;
  1524. error = scsi_xs_sync(xs);
  1525. scsi_xs_put(xs);
  1526. if (error == 0) {
  1527. a->lrpcs.type = (buf[4] >> 6) & 3;
  1528. a->lrpcs.vra = (buf[4] >> 3) & 7;
  1529. a->lrpcs.ucca = (buf[4]) & 7;
  1530. a->lrpcs.region_mask = buf[5];
  1531. a->lrpcs.rpc_scheme = buf[6];
  1532. }
  1533. break;
  1534. case DVD_HOST_SEND_RPC_STATE:
  1535. cmd->opcode = GPCMD_SEND_KEY;
  1536. cmd->bytes[8] = 8;
  1537. cmd->bytes[9] = 6 | (0 << 6);
  1538. buf[1] = 6;
  1539. buf[4] = a->hrpcs.pdrc;
  1540. xs->datalen = 8;
  1541. xs->flags |= SCSI_DATA_OUT;
  1542. error = scsi_xs_sync(xs);
  1543. scsi_xs_put(xs);
  1544. break;
  1545. default:
  1546. scsi_xs_put(xs);
  1547. error = ENOTTY;
  1548. break;
  1549. }
  1550. done:
  1551. dma_free(buf, DVD_AUTH_BUFSIZE);
  1552. return (error);
  1553. }
  1554. #define DVD_READ_PHYSICAL_BUFSIZE (4 + 4 * 20)
  1555. int
  1556. dvd_read_physical(struct cd_softc *sc, union dvd_struct *s)
  1557. {
  1558. struct scsi_generic *cmd;
  1559. struct dvd_layer *layer;
  1560. struct scsi_xfer *xs;
  1561. u_int8_t *buf, *bufp;
  1562. int error, i;
  1563. buf = dma_alloc(DVD_READ_PHYSICAL_BUFSIZE, PR_WAITOK | PR_ZERO);
  1564. if (buf == NULL)
  1565. return (ENOMEM);
  1566. xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN);
  1567. if (xs == NULL) {
  1568. error = ENOMEM;
  1569. goto done;
  1570. }
  1571. xs->cmdlen = sizeof(*cmd);
  1572. xs->data = buf;
  1573. xs->datalen = DVD_READ_PHYSICAL_BUFSIZE;
  1574. xs->timeout = 30000;
  1575. cmd = xs->cmd;
  1576. cmd->opcode = GPCMD_READ_DVD_STRUCTURE;
  1577. cmd->bytes[6] = s->type;
  1578. _lto2b(xs->datalen, &cmd->bytes[7]);
  1579. cmd->bytes[5] = s->physical.layer_num;
  1580. error = scsi_xs_sync(xs);
  1581. scsi_xs_put(xs);
  1582. if (error == 0) {
  1583. for (i = 0, bufp = &buf[4], layer = &s->physical.layer[0];
  1584. i < 4; i++, bufp += 20, layer++) {
  1585. bzero(layer, sizeof(*layer));
  1586. layer->book_version = bufp[0] & 0xf;
  1587. layer->book_type = bufp[0] >> 4;
  1588. layer->min_rate = bufp[1] & 0xf;
  1589. layer->disc_size = bufp[1] >> 4;
  1590. layer->layer_type = bufp[2] & 0xf;
  1591. layer->track_path = (bufp[2] >> 4) & 1;
  1592. layer->nlayers = (bufp[2] >> 5) & 3;
  1593. layer->track_density = bufp[3] & 0xf;
  1594. layer->linear_density = bufp[3] >> 4;
  1595. layer->start_sector = _4btol(&bufp[4]);
  1596. layer->end_sector = _4btol(&bufp[8]);
  1597. layer->end_sector_l0 = _4btol(&bufp[12]);
  1598. layer->bca = bufp[16] >> 7;
  1599. }
  1600. }
  1601. done:
  1602. dma_free(buf, DVD_READ_PHYSICAL_BUFSIZE);
  1603. return (error);
  1604. }
  1605. #define DVD_READ_COPYRIGHT_BUFSIZE 8
  1606. int
  1607. dvd_read_copyright(struct cd_softc *sc, union dvd_struct *s)
  1608. {
  1609. struct scsi_generic *cmd;
  1610. struct scsi_xfer *xs;
  1611. u_int8_t *buf;
  1612. int error;
  1613. buf = dma_alloc(DVD_READ_COPYRIGHT_BUFSIZE, PR_WAITOK | PR_ZERO);
  1614. if (buf == NULL)
  1615. return (ENOMEM);
  1616. xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN);
  1617. if (xs == NULL) {
  1618. error = ENOMEM;
  1619. goto done;
  1620. }
  1621. xs->cmdlen = sizeof(*cmd);
  1622. xs->data = buf;
  1623. xs->datalen = DVD_READ_COPYRIGHT_BUFSIZE;
  1624. xs->timeout = 30000;
  1625. cmd = xs->cmd;
  1626. cmd->opcode = GPCMD_READ_DVD_STRUCTURE;
  1627. cmd->bytes[6] = s->type;
  1628. _lto2b(xs->datalen, &cmd->bytes[7]);
  1629. cmd->bytes[5] = s->copyright.layer_num;
  1630. error = scsi_xs_sync(xs);
  1631. scsi_xs_put(xs);
  1632. if (error == 0) {
  1633. s->copyright.cpst = buf[4];
  1634. s->copyright.rmi = buf[5];
  1635. }
  1636. done:
  1637. dma_free(buf, DVD_READ_COPYRIGHT_BUFSIZE);
  1638. return (error);
  1639. }
  1640. int
  1641. dvd_read_disckey(struct cd_softc *sc, union dvd_struct *s)
  1642. {
  1643. struct scsi_read_dvd_structure_data *buf;
  1644. struct scsi_read_dvd_structure *cmd;
  1645. struct scsi_xfer *xs;
  1646. int error;
  1647. buf = dma_alloc(sizeof(*buf), PR_WAITOK | PR_ZERO);
  1648. if (buf == NULL)
  1649. return (ENOMEM);
  1650. xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN);
  1651. if (xs == NULL) {
  1652. error = ENOMEM;
  1653. goto done;
  1654. }
  1655. xs->cmdlen = sizeof(*cmd);
  1656. xs->data = (void *)buf;
  1657. xs->datalen = sizeof(*buf);
  1658. xs->timeout = 30000;
  1659. cmd = (struct scsi_read_dvd_structure *)xs->cmd;
  1660. cmd->opcode = GPCMD_READ_DVD_STRUCTURE;
  1661. cmd->format = s->type;
  1662. cmd->agid = s->disckey.agid << 6;
  1663. _lto2b(xs->datalen, cmd->length);
  1664. error = scsi_xs_sync(xs);
  1665. scsi_xs_put(xs);
  1666. if (error == 0)
  1667. memcpy(s->disckey.value, buf->data, sizeof(s->disckey.value));
  1668. done:
  1669. dma_free(buf, sizeof(*buf));
  1670. return (error);
  1671. }
  1672. #define DVD_READ_BCA_BUFLEN (4 + 188)
  1673. int
  1674. dvd_read_bca(struct cd_softc *sc, union dvd_struct *s)
  1675. {
  1676. struct scsi_generic *cmd;
  1677. struct scsi_xfer *xs;
  1678. u_int8_t *buf;
  1679. int error;
  1680. buf = dma_alloc(DVD_READ_BCA_BUFLEN, PR_WAITOK | PR_ZERO);
  1681. if (buf == NULL)
  1682. return (ENOMEM);
  1683. xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN);
  1684. if (xs == NULL) {
  1685. error = ENOMEM;
  1686. goto done;
  1687. }
  1688. xs->cmdlen = sizeof(*cmd);
  1689. xs->data = buf;
  1690. xs->datalen = DVD_READ_BCA_BUFLEN;
  1691. xs->timeout = 30000;
  1692. cmd = xs->cmd;
  1693. cmd->opcode = GPCMD_READ_DVD_STRUCTURE;
  1694. cmd->bytes[6] = s->type;
  1695. _lto2b(xs->datalen, &cmd->bytes[7]);
  1696. error = scsi_xs_sync(xs);
  1697. scsi_xs_put(xs);
  1698. if (error == 0) {
  1699. s->bca.len = _2btol(&buf[0]);
  1700. if (s->bca.len < 12 || s->bca.len > 188)
  1701. return (EIO);
  1702. memcpy(s->bca.value, &buf[4], s->bca.len);
  1703. }
  1704. done:
  1705. dma_free(buf, DVD_READ_BCA_BUFLEN);
  1706. return (error);
  1707. }
  1708. int
  1709. dvd_read_manufact(struct cd_softc *sc, union dvd_struct *s)
  1710. {
  1711. struct scsi_read_dvd_structure_data *buf;
  1712. struct scsi_read_dvd_structure *cmd;
  1713. struct scsi_xfer *xs;
  1714. int error;
  1715. buf = dma_alloc(sizeof(*buf), PR_WAITOK | PR_ZERO);
  1716. if (buf == NULL)
  1717. return (ENOMEM);
  1718. xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN);
  1719. if (xs == NULL) {
  1720. error = ENOMEM;
  1721. goto done;
  1722. }
  1723. xs->cmdlen = sizeof(*cmd);
  1724. xs->data = (void *)buf;
  1725. xs->datalen = sizeof(*buf);
  1726. xs->timeout = 30000;
  1727. cmd = (struct scsi_read_dvd_structure *)xs->cmd;
  1728. cmd->opcode = GPCMD_READ_DVD_STRUCTURE;
  1729. cmd->format = s->type;
  1730. _lto2b(xs->datalen, cmd->length);
  1731. error = scsi_xs_sync(xs);
  1732. scsi_xs_put(xs);
  1733. if (error == 0) {
  1734. s->manufact.len = _2btol(buf->len);
  1735. if (s->manufact.len >= 0 && s->manufact.len <= 2048)
  1736. memcpy(s->manufact.value, buf->data, s->manufact.len);
  1737. else
  1738. error = EIO;
  1739. }
  1740. done:
  1741. dma_free(buf, sizeof(*buf));
  1742. return (error);
  1743. }
  1744. int
  1745. dvd_read_struct(struct cd_softc *sc, union dvd_struct *s)
  1746. {
  1747. switch (s->type) {
  1748. case DVD_STRUCT_PHYSICAL:
  1749. return (dvd_read_physical(sc, s));
  1750. case DVD_STRUCT_COPYRIGHT:
  1751. return (dvd_read_copyright(sc, s));
  1752. case DVD_STRUCT_DISCKEY:
  1753. return (dvd_read_disckey(sc, s));
  1754. case DVD_STRUCT_BCA:
  1755. return (dvd_read_bca(sc, s));
  1756. case DVD_STRUCT_MANUFACT:
  1757. return (dvd_read_manufact(sc, s));
  1758. default:
  1759. return (EINVAL);
  1760. }
  1761. }
  1762. int
  1763. cd_interpret_sense(struct scsi_xfer *xs)
  1764. {
  1765. struct scsi_sense_data *sense = &xs->sense;
  1766. struct scsi_link *sc_link = xs->sc_link;
  1767. u_int8_t skey = sense->flags & SSD_KEY;
  1768. u_int8_t serr = sense->error_code & SSD_ERRCODE;
  1769. if (((sc_link->flags & SDEV_OPEN) == 0) ||
  1770. (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED))
  1771. return (scsi_interpret_sense(xs));
  1772. /*
  1773. * We do custom processing in cd for the unit becoming ready
  1774. * case. We do not allow xs->retries to be decremented on the
  1775. * "Unit Becoming Ready" case. This is because CD drives
  1776. * report "Unit Becoming Ready" when loading media and can
  1777. * take a long time. Rather than having a massive timeout for
  1778. * all operations (which would cause other problems), we allow
  1779. * operations to wait (but be interruptable with Ctrl-C)
  1780. * forever as long as the drive is reporting that it is
  1781. * becoming ready. All other cases of not being ready are
  1782. * handled by the default handler.
  1783. */
  1784. switch(skey) {
  1785. case SKEY_NOT_READY:
  1786. if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
  1787. return (0);
  1788. if (ASC_ASCQ(sense) == SENSE_NOT_READY_BECOMING_READY) {
  1789. SC_DEBUG(sc_link, SDEV_DB1, ("not ready: busy (%#x)\n",
  1790. sense->add_sense_code_qual));
  1791. /* don't count this as a retry */
  1792. xs->retries++;
  1793. return (scsi_delay(xs, 1));
  1794. }
  1795. break;
  1796. /* XXX more to come here for a few other cases */
  1797. default:
  1798. break;
  1799. }
  1800. return (scsi_interpret_sense(xs));
  1801. }
  1802. /*
  1803. * Find out from the device what its capacity is.
  1804. */
  1805. u_int64_t
  1806. cd_size(struct scsi_link *sc_link, int flags, u_int32_t *blksize)
  1807. {
  1808. struct scsi_read_cap_data_16 *rdcap16;
  1809. struct scsi_read_capacity_16 *cmd;
  1810. struct scsi_read_cap_data *rdcap;
  1811. struct scsi_read_capacity *cmd10;
  1812. struct scsi_xfer *xs;
  1813. u_int64_t max_addr;
  1814. int error;
  1815. if (blksize != NULL)
  1816. *blksize = 0;
  1817. CLR(flags, SCSI_IGNORE_ILLEGAL_REQUEST);
  1818. /*
  1819. * Start with a READ CAPACITY(10).
  1820. */
  1821. rdcap = dma_alloc(sizeof(*rdcap), ((flags & SCSI_NOSLEEP) ?
  1822. PR_NOWAIT : PR_WAITOK) | PR_ZERO);
  1823. if (rdcap == NULL)
  1824. return (0);
  1825. xs = scsi_xs_get(sc_link, flags | SCSI_DATA_IN | SCSI_SILENT);
  1826. if (xs == NULL) {
  1827. dma_free(rdcap, sizeof(*rdcap));
  1828. return (0);
  1829. }
  1830. xs->cmdlen = sizeof(*cmd10);
  1831. xs->data = (void *)rdcap;
  1832. xs->datalen = sizeof(*rdcap);
  1833. xs->timeout = 20000;
  1834. cmd10 = (struct scsi_read_capacity *)xs->cmd;
  1835. cmd10->opcode = READ_CAPACITY;
  1836. error = scsi_xs_sync(xs);
  1837. scsi_xs_put(xs);
  1838. if (error) {
  1839. SC_DEBUG(sc_link, SDEV_DB1, ("READ CAPACITY error (%#x)\n",
  1840. error));
  1841. dma_free(rdcap, sizeof(*rdcap));
  1842. return (0);
  1843. }
  1844. max_addr = _4btol(rdcap->addr);
  1845. if (blksize != NULL)
  1846. *blksize = _4btol(rdcap->length);
  1847. dma_free(rdcap, sizeof(*rdcap));
  1848. if (SCSISPC(sc_link->inqdata.version) < 3 && max_addr != 0xffffffff)
  1849. goto exit;
  1850. /*
  1851. * SCSI-3 devices, or devices reporting more than 2^32-1 sectors can
  1852. * try READ CAPACITY(16).
  1853. */
  1854. rdcap16 = dma_alloc(sizeof(*rdcap16), ((flags & SCSI_NOSLEEP) ?
  1855. PR_NOWAIT : PR_WAITOK) | PR_ZERO);
  1856. if (rdcap16 == NULL)
  1857. goto exit;
  1858. xs = scsi_xs_get(sc_link, flags | SCSI_DATA_IN | SCSI_SILENT);
  1859. if (xs == NULL) {
  1860. dma_free(rdcap16, sizeof(*rdcap16));
  1861. goto exit;
  1862. }
  1863. xs->cmdlen = sizeof(*cmd);
  1864. xs->data = (void *)rdcap16;
  1865. xs->datalen = sizeof(*rdcap16);
  1866. xs->timeout = 20000;
  1867. cmd = (struct scsi_read_capacity_16 *)xs->cmd;
  1868. cmd->opcode = READ_CAPACITY_16;
  1869. cmd->byte2 = SRC16_SERVICE_ACTION;
  1870. _lto4b(sizeof(*rdcap16), cmd->length);
  1871. error = scsi_xs_sync(xs);
  1872. scsi_xs_put(xs);
  1873. if (error) {
  1874. SC_DEBUG(sc_link, SDEV_DB1, ("READ CAPACITY 16 error (%#x)\n",
  1875. error));
  1876. dma_free(rdcap16, sizeof(*rdcap16));
  1877. goto exit;
  1878. }
  1879. max_addr = _8btol(rdcap16->addr);
  1880. if (blksize != NULL)
  1881. *blksize = _4btol(rdcap16->length);
  1882. /* XXX The other READ CAPACITY(16) info could be stored away. */
  1883. dma_free(rdcap16, sizeof(*rdcap16));
  1884. return (max_addr + 1);
  1885. exit:
  1886. /* Return READ CAPACITY 10 values. */
  1887. if (max_addr != 0xffffffff)
  1888. return (max_addr + 1);
  1889. else if (blksize != NULL)
  1890. *blksize = 0;
  1891. return (0);
  1892. }
  1893. #if defined(__macppc__)
  1894. int
  1895. cd_eject(void)
  1896. {
  1897. struct cd_softc *sc;
  1898. int error = 0;
  1899. if (cd_cd.cd_ndevs == 0 || (sc = cd_cd.cd_devs[0]) == NULL)
  1900. return (ENXIO);
  1901. if ((error = disk_lock(&sc->sc_dk)) != 0)
  1902. return (error);
  1903. if (sc->sc_dk.dk_openmask == 0) {
  1904. sc->sc_link->flags |= SDEV_EJECTING;
  1905. scsi_prevent(sc->sc_link, PR_ALLOW,
  1906. SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY |
  1907. SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE);
  1908. sc->sc_link->flags &= ~SDEV_MEDIA_LOADED;
  1909. scsi_start(sc->sc_link, SSS_STOP|SSS_LOEJ, 0);
  1910. sc->sc_link->flags &= ~SDEV_EJECTING;
  1911. }
  1912. disk_unlock(&sc->sc_dk);
  1913. return (error);
  1914. }
  1915. #endif