gdrom.c revision 1.39 1 /* $NetBSD: gdrom.c,v 1.39 2014/07/25 08:02:18 dholland Exp $ */
2
3 /*-
4 * Copyright (c) 2001 Marcus Comstedt
5 * 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 Marcus Comstedt.
18 * 4. Neither the name of The NetBSD Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
36 __KERNEL_RCSID(0, "$NetBSD: gdrom.c,v 1.39 2014/07/25 08:02:18 dholland Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41
42 #include <sys/buf.h>
43 #include <sys/bufq.h>
44 #include <sys/ioctl.h>
45 #include <sys/fcntl.h>
46 #include <sys/disklabel.h>
47 #include <sys/disk.h>
48 #include <sys/cdio.h>
49 #include <sys/proc.h>
50 #include <sys/conf.h>
51
52 #include <machine/sysasicvar.h>
53
54 #include "ioconf.h"
55
56 static int gdrommatch(device_t, cfdata_t, void *);
57 static void gdromattach(device_t, device_t, void *);
58
59 dev_type_open(gdromopen);
60 dev_type_close(gdromclose);
61 dev_type_read(gdromread);
62 dev_type_write(gdromwrite);
63 dev_type_ioctl(gdromioctl);
64 dev_type_strategy(gdromstrategy);
65
66 const struct bdevsw gdrom_bdevsw = {
67 .d_open = gdromopen,
68 .d_close = gdromclose,
69 .d_strategy = gdromstrategy,
70 .d_ioctl = gdromioctl,
71 .d_dump = nodump,
72 .d_psize = nosize,
73 .d_discard = nodiscard,
74 .d_flag = D_DISK
75 };
76
77 const struct cdevsw gdrom_cdevsw = {
78 .d_open = gdromopen,
79 .d_close = gdromclose,
80 .d_read = gdromread,
81 .d_write = gdromwrite,
82 .d_ioctl = gdromioctl,
83 .d_stop = nostop,
84 .d_tty = notty,
85 .d_poll = nopoll,
86 .d_mmap = nommap,
87 .d_kqfilter = nokqfilter,
88 .d_flag = D_DISK
89 };
90
91 struct gdrom_softc {
92 device_t sc_dev; /* generic device info */
93 struct disk sc_dk; /* generic disk info */
94 struct bufq_state *sc_bufq; /* device buffer queue */
95 struct buf curbuf; /* state of current I/O operation */
96
97 bool is_open;
98 bool is_busy;
99 bool is_active;
100 int openpart_start; /* start sector of currently open partition */
101
102 int cmd_active;
103 void *cmd_result_buf; /* where to store result data (16 bit aligned) */
104 int cmd_result_size; /* number of bytes allocated for buf */
105 int cmd_actual; /* number of bytes actually read */
106 int cmd_cond; /* resulting condition of command */
107 };
108
109 CFATTACH_DECL_NEW(gdrom, sizeof(struct gdrom_softc),
110 gdrommatch, gdromattach, NULL, NULL);
111
112 struct dkdriver gdromdkdriver = { gdromstrategy };
113
114
115 struct gd_toc {
116 unsigned int entry[99];
117 unsigned int first, last;
118 unsigned int leadout;
119 };
120
121 #ifdef GDROMDEBUG
122 #define DPRINTF(x) printf x
123 #else
124 #define DPRINTF(x) /**/
125 #endif
126
127 #define TOC_LBA(n) ((n) & 0xffffff00)
128 #define TOC_ADR(n) ((n) & 0x0f)
129 #define TOC_CTRL(n) (((n) & 0xf0) >> 4)
130 #define TOC_TRACK(n) (((n) & 0x0000ff00) >> 8)
131
132 #define GDROM(o) (*(volatile uint8_t *)(0xa05f7000 + (o)))
133
134 #define GDSTATSTAT(n) ((n) & 0xf)
135 #define GDSTATDISK(n) (((n) >> 4) & 0xf)
136
137 #define GDROM_BUSY GDROM(0x18)
138 #define GDROM_DATA (*(volatile uint16_t *)(&GDROM(0x80)))
139 #define GDROM_REGX GDROM(0x84)
140 #define GDROM_STAT GDROM(0x8c)
141 #define GDROM_CNTLO GDROM(0x90)
142 #define GDROM_CNTHI GDROM(0x94)
143 #define GDROM_COND GDROM(0x9c)
144
145 #if 0
146 static int gdrom_getstat(void);
147 #endif
148 static int gdrom_do_command(struct gdrom_softc *, void *, void *,
149 unsigned int, int *);
150 static int gdrom_command_sense(struct gdrom_softc *, void *, void *,
151 unsigned int, int *);
152 static int gdrom_read_toc(struct gdrom_softc *, struct gd_toc *);
153 static int gdrom_read_sectors(struct gdrom_softc *, void *, int, int,
154 int *);
155 static int gdrom_mount_disk(struct gdrom_softc *);
156 static int gdrom_intr(void *);
157 static void gdrom_start(struct gdrom_softc *);
158
159 #if 0
160 int
161 gdrom_getstat(void)
162 {
163 uint8_t s1, s2, s3;
164
165 if (GDROM_BUSY & 0x80)
166 return -1;
167 s1 = GDROM_STAT;
168 s2 = GDROM_STAT;
169 s3 = GDROM_STAT;
170 if (GDROM_BUSY & 0x80)
171 return -1;
172 if (s1 == s2)
173 return s1;
174 else if (s2 == s3)
175 return s2;
176 else
177 return -1;
178 }
179 #endif
180
181 int
182 gdrom_intr(void *arg)
183 {
184 struct gdrom_softc *sc = arg;
185 int s;
186 uint8_t cond;
187
188 s = splbio();
189 cond = GDROM_COND;
190 DPRINTF(("GDROM: cond = %x\n", cond));
191 if (!sc->cmd_active) {
192 DPRINTF(("GDROM: inactive IRQ!?\n"));
193 splx(s);
194 return 0;
195 }
196
197 if ((cond & 0x08) != 0) {
198 int cnt = (GDROM_CNTHI << 8) | GDROM_CNTLO;
199 DPRINTF(("GDROM: cnt = %d\n", cnt));
200 sc->cmd_actual += cnt;
201 if (cnt > 0 && sc->cmd_result_size > 0) {
202 int subcnt = (cnt > sc->cmd_result_size ?
203 sc->cmd_result_size : cnt);
204 uint16_t *ptr = sc->cmd_result_buf;
205 sc->cmd_result_buf = ((uint8_t *)sc->cmd_result_buf) +
206 subcnt;
207 sc->cmd_result_size -= subcnt;
208 cnt -= subcnt;
209 while (subcnt > 0) {
210 *ptr++ = GDROM_DATA;
211 subcnt -= 2;
212 }
213 }
214 while (cnt > 0) {
215 (void)GDROM_DATA;
216 cnt -= 2;
217 }
218 }
219 while ((GDROM_BUSY & 0x80) != 0);
220
221 if ((cond & 0x08) == 0) {
222 sc->cmd_cond = cond;
223 sc->cmd_active = 0;
224 wakeup(&sc->cmd_active);
225 }
226
227 splx(s);
228 return 1;
229 }
230
231
232 int
233 gdrom_do_command(struct gdrom_softc *sc, void *req, void *buf,
234 unsigned int nbyt, int *resid)
235 {
236 int i, s;
237 uint16_t *ptr = req;
238
239 while (GDROM_BUSY & 0x88)
240 ;
241 if (buf != NULL) {
242 GDROM_CNTLO = nbyt & 0xff;
243 GDROM_CNTHI = (nbyt >> 8) & 0xff;
244 GDROM_REGX = 0;
245 }
246 sc->cmd_result_buf = buf;
247 sc->cmd_result_size = nbyt;
248
249 if (GDSTATSTAT(GDROM_STAT) == 0x06)
250 return -1;
251
252 GDROM_COND = 0xa0;
253 DELAY(1);
254 while ((GDROM_BUSY & 0x88) != 0x08)
255 ;
256
257 s = splbio();
258
259 sc->cmd_actual = 0;
260 sc->cmd_active = 1;
261
262 for (i = 0; i < 6; i++)
263 GDROM_DATA = ptr[i];
264
265 while (sc->cmd_active)
266 tsleep(&sc->cmd_active, PRIBIO, "gdrom", 0);
267
268 splx(s);
269
270 if (resid != NULL)
271 *resid = sc->cmd_result_size;
272
273 return sc->cmd_cond;
274 }
275
276
277 int gdrom_command_sense(struct gdrom_softc *sc, void *req, void *buf,
278 unsigned int nbyt, int *resid)
279 {
280 /*
281 * 76543210 76543210
282 * 0 0x13 -
283 * 2 - bufsz(hi)
284 * 4 bufsz(lo) -
285 * 6 - -
286 * 8 - -
287 * 10 - -
288 */
289 uint16_t sense_data[5];
290 uint8_t cmd[12];
291 int cond, sense_key, sense_specific;
292
293 cond = gdrom_do_command(sc, req, buf, nbyt, resid);
294
295 if (cond < 0) {
296 DPRINTF(("GDROM: not ready (2:58)\n"));
297 return EIO;
298 }
299
300 if ((cond & 1) == 0) {
301 DPRINTF(("GDROM: no sense. 0:0\n"));
302 return 0;
303 }
304
305 memset(cmd, 0, sizeof(cmd));
306
307 cmd[0] = 0x13;
308 cmd[4] = sizeof(sense_data);
309
310 gdrom_do_command(sc, cmd, sense_data, sizeof(sense_data), NULL);
311
312 sense_key = sense_data[1] & 0xf;
313 sense_specific = sense_data[4];
314 if (sense_key == 11 && sense_specific == 0) {
315 DPRINTF(("GDROM: aborted (ignored). 0:0\n"));
316 return 0;
317 }
318
319 DPRINTF(("GDROM: SENSE %d:", sense_key));
320 DPRINTF(("GDROM: %d\n", sense_specific));
321
322 return sense_key == 0 ? 0 : EIO;
323 }
324
325 int gdrom_read_toc(struct gdrom_softc *sc, struct gd_toc *toc)
326 {
327 /*
328 * 76543210 76543210
329 * 0 0x14 -
330 * 2 - bufsz(hi)
331 * 4 bufsz(lo) -
332 * 6 - -
333 * 8 - -
334 * 10 - -
335 */
336 uint8_t cmd[12];
337
338 memset(cmd, 0, sizeof(cmd));
339
340 cmd[0] = 0x14;
341 cmd[3] = sizeof(struct gd_toc) >> 8;
342 cmd[4] = sizeof(struct gd_toc) & 0xff;
343
344 return gdrom_command_sense(sc, cmd, toc, sizeof(struct gd_toc), NULL);
345 }
346
347 int gdrom_read_sectors(struct gdrom_softc *sc, void *buf, int sector, int cnt,
348 int *resid)
349 {
350 /*
351 * 76543210 76543210
352 * 0 0x30 datafmt
353 * 2 sec(hi) sec(mid)
354 * 4 sec(lo) -
355 * 6 - -
356 * 8 cnt(hi) cnt(mid)
357 * 10 cnt(lo) -
358 */
359 uint8_t cmd[12];
360
361 memset(cmd, 0, sizeof(cmd));
362
363 cmd[0] = 0x30;
364 cmd[1] = 0x20;
365 cmd[2] = sector >> 16;
366 cmd[3] = sector >> 8;
367 cmd[4] = sector;
368 cmd[8] = cnt >> 16;
369 cmd[9] = cnt >> 8;
370 cmd[10] = cnt;
371
372 return gdrom_command_sense(sc, cmd, buf, cnt << 11, resid);
373 }
374
375 int gdrom_mount_disk(struct gdrom_softc *sc)
376 {
377 /*
378 * 76543210 76543210
379 * 0 0x70 -
380 * 2 0x1f -
381 * 4 - -
382 * 6 - -
383 * 8 - -
384 * 10 - -
385 */
386 uint8_t cmd[12];
387
388 memset(cmd, 0, sizeof(cmd));
389
390 cmd[0] = 0x70;
391 cmd[1] = 0x1f;
392
393 return gdrom_command_sense(sc, cmd, NULL, 0, NULL);
394 }
395
396 int
397 gdrommatch(device_t parent, cfdata_t cf, void *aux)
398 {
399 static int gdrom_matched = 0;
400
401 /* Allow only once instance. */
402 if (gdrom_matched)
403 return 0;
404 gdrom_matched = 1;
405
406 return 1;
407 }
408
409 void
410 gdromattach(device_t parent, device_t self, void *aux)
411 {
412 struct gdrom_softc *sc;
413 uint32_t p;
414
415 sc = device_private(self);
416 sc->sc_dev = self;
417
418 bufq_alloc(&sc->sc_bufq, "disksort", BUFQ_SORT_RAWBLOCK);
419
420 /*
421 * Initialize and attach the disk structure.
422 */
423 disk_init(&sc->sc_dk, device_xname(self), &gdromdkdriver);
424 disk_attach(&sc->sc_dk);
425
426 /*
427 * reenable disabled drive
428 */
429 *((volatile uint32_t *)0xa05f74e4) = 0x1fffff;
430 for (p = 0; p < 0x200000 / 4; p++)
431 (void)((volatile uint32_t *)0xa0000000)[p];
432
433 printf(": %s\n", sysasic_intr_string(SYSASIC_IRL9));
434 sysasic_intr_establish(SYSASIC_EVENT_GDROM, IPL_BIO, SYSASIC_IRL9,
435 gdrom_intr, sc);
436 }
437
438 int
439 gdromopen(dev_t dev, int flags, int devtype, struct lwp *l)
440 {
441 struct gdrom_softc *sc;
442 int s, error, unit, cnt;
443 struct gd_toc toc;
444
445 DPRINTF(("GDROM: open\n"));
446
447 unit = DISKUNIT(dev);
448
449 sc = device_lookup_private(&gdrom_cd, unit);
450 if (sc == NULL)
451 return ENXIO;
452
453 if (sc->is_open)
454 return EBUSY;
455
456 s = splbio();
457 while (sc->is_busy)
458 tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
459 sc->is_busy = true;
460 splx(s);
461
462 for (cnt = 0; cnt < 5; cnt++)
463 if ((error = gdrom_mount_disk(sc)) == 0)
464 break;
465
466 if (error == 0)
467 error = gdrom_read_toc(sc, &toc);
468
469 sc->is_busy = false;
470 wakeup(&sc->is_busy);
471
472 if (error != 0)
473 return error;
474
475 sc->is_open = true;
476 sc->openpart_start = 150;
477
478 DPRINTF(("GDROM: open OK\n"));
479 return 0;
480 }
481
482 int
483 gdromclose(dev_t dev, int flags, int devtype, struct lwp *l)
484 {
485 struct gdrom_softc *sc;
486 int unit;
487
488 DPRINTF(("GDROM: close\n"));
489
490 unit = DISKUNIT(dev);
491 sc = device_lookup_private(&gdrom_cd, unit);
492
493 sc->is_open = false;
494
495 return 0;
496 }
497
498 void
499 gdromstrategy(struct buf *bp)
500 {
501 struct gdrom_softc *sc;
502 int s, unit;
503
504 DPRINTF(("GDROM: strategy\n"));
505
506 unit = DISKUNIT(bp->b_dev);
507 sc = device_lookup_private(&gdrom_cd, unit);
508
509 if (bp->b_bcount == 0)
510 goto done;
511
512 bp->b_rawblkno = bp->b_blkno / (2048 / DEV_BSIZE) + sc->openpart_start;
513
514 DPRINTF(("GDROM: read_sectors(%p, %lld, %d) [%d bytes]\n",
515 bp->b_data, bp->b_rawblkno,
516 bp->b_bcount >> 11, bp->b_bcount));
517
518 s = splbio();
519 bufq_put(sc->sc_bufq, bp);
520 splx(s);
521 if (!sc->is_active)
522 gdrom_start(sc);
523 return;
524
525 done:
526 bp->b_resid = bp->b_bcount;
527 biodone(bp);
528 }
529
530 void
531 gdrom_start(struct gdrom_softc *sc)
532 {
533 struct buf *bp;
534 int error, resid, s;
535
536 sc->is_active = true;
537
538 for (;;) {
539 s = splbio();
540 bp = bufq_get(sc->sc_bufq);
541 if (bp == NULL) {
542 splx(s);
543 break;
544 }
545
546 while (sc->is_busy)
547 tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
548 sc->is_busy = true;
549 disk_busy(&sc->sc_dk);
550 splx(s);
551
552 error = gdrom_read_sectors(sc, bp->b_data, bp->b_rawblkno,
553 bp->b_bcount >> 11, &resid);
554 bp->b_error = error;
555 bp->b_resid = resid;
556 if (error != 0)
557 bp->b_resid = bp->b_bcount;
558
559 sc->is_busy = false;
560 wakeup(&sc->is_busy);
561
562 s = splbio();
563 disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid,
564 (bp->b_flags & B_READ) != 0);
565 splx(s);
566 biodone(bp);
567 }
568
569 sc->is_active = false;
570 }
571
572 int
573 gdromioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
574 {
575 struct gdrom_softc *sc;
576 int unit, error;
577
578 DPRINTF(("GDROM: ioctl %lx\n", cmd));
579
580 unit = DISKUNIT(dev);
581 sc = device_lookup_private(&gdrom_cd, unit);
582
583 switch (cmd) {
584 case CDIOREADMSADDR: {
585 int s, track, sessno = *(int *)addr;
586 struct gd_toc toc;
587
588 if (sessno != 0)
589 return EINVAL;
590
591 s = splbio();
592 while (sc->is_busy)
593 tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
594 sc->is_busy = true;
595 splx(s);
596
597 error = gdrom_read_toc(sc, &toc);
598
599 sc->is_busy = false;
600 wakeup(&sc->is_busy);
601
602 if (error != 0)
603 return error;
604 #ifdef GDROMDEBUGTOC
605 { /* Dump the GDROM TOC */
606 unsigned char *ptr = (unsigned char *)&toc;
607 int i;
608
609 printf("gdrom: TOC\n");
610 for(i = 0; i < sizeof(toc); ++i) {
611 printf("%02x", *ptr++);
612 if( i%32 == 31)
613 printf("\n");
614 else if( i%4 == 3)
615 printf(",");
616 }
617 printf("\n");
618 }
619 #endif
620 for (track = TOC_TRACK(toc.last);
621 track >= TOC_TRACK(toc.first);
622 --track) {
623 if (track < 1 || track > 100)
624 return ENXIO;
625 if (TOC_CTRL(toc.entry[track - 1]))
626 break;
627 }
628
629 #ifdef GDROMDEBUGTOC
630 printf("gdrom: Using track %d, LBA %u\n", track,
631 TOC_LBA(toc.entry[track - 1]));
632 #endif
633
634 *(int *)addr = htonl(TOC_LBA(toc.entry[track - 1])) -
635 sc->openpart_start;
636
637 return 0;
638 }
639 default:
640 return ENOTTY;
641 }
642
643 #ifdef DIAGNOSTIC
644 panic("gdromioctl: impossible");
645 #endif
646 }
647
648
649 int
650 gdromread(dev_t dev, struct uio *uio, int flags)
651 {
652
653 DPRINTF(("GDROM: read\n"));
654 return physio(gdromstrategy, NULL, dev, B_READ, minphys, uio);
655 }
656
657 int
658 gdromwrite(dev_t dev, struct uio *uio, int flags)
659 {
660
661 return EROFS;
662 }
663