gdrom.c revision 1.30 1 /* $NetBSD: gdrom.c,v 1.30 2010/09/01 15:08:22 tsutsui 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.30 2010/09/01 15:08:22 tsutsui 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 int gdrommatch(device_t, cfdata_t, void *);
57 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 gdromopen, gdromclose, gdromstrategy, gdromioctl, nodump,
68 nosize, D_DISK
69 };
70
71 const struct cdevsw gdrom_cdevsw = {
72 gdromopen, gdromclose, gdromread, gdromwrite, gdromioctl,
73 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
74 };
75
76 struct gdrom_softc {
77 device_t sc_dev; /* generic device info */
78 struct disk sc_dk; /* generic disk info */
79 struct bufq_state *sc_bufq; /* device buffer queue */
80 struct buf curbuf; /* state of current I/O operation */
81
82 int is_open, is_busy;
83 bool is_active;
84 int openpart_start; /* start sector of currently open partition */
85
86 int cmd_active;
87 void *cmd_result_buf; /* where to store result data (16 bit aligned) */
88 int cmd_result_size; /* number of bytes allocated for buf */
89 int cmd_actual; /* number of bytes actually read */
90 int cmd_cond; /* resulting condition of command */
91 };
92
93 CFATTACH_DECL_NEW(gdrom, sizeof(struct gdrom_softc),
94 gdrommatch, gdromattach, NULL, NULL);
95
96 struct dkdriver gdromdkdriver = { gdromstrategy };
97
98
99 struct gd_toc {
100 unsigned int entry[99];
101 unsigned int first, last;
102 unsigned int leadout;
103 };
104
105 #define TOC_LBA(n) ((n) & 0xffffff00)
106 #define TOC_ADR(n) ((n) & 0x0f)
107 #define TOC_CTRL(n) (((n) & 0xf0) >> 4)
108 #define TOC_TRACK(n) (((n) & 0x0000ff00) >> 8)
109
110 #define GDROM(o) (*(volatile unsigned char *)(0xa05f7000 + (o)))
111
112 #define GDSTATSTAT(n) ((n) & 0xf)
113 #define GDSTATDISK(n) (((n) >> 4) & 0xf)
114
115 #define GDROM_BUSY GDROM(0x18)
116 #define GDROM_DATA (*(volatile short *) & GDROM(0x80))
117 #define GDROM_REGX GDROM(0x84)
118 #define GDROM_STAT GDROM(0x8c)
119 #define GDROM_CNTLO GDROM(0x90)
120 #define GDROM_CNTHI GDROM(0x94)
121 #define GDROM_COND GDROM(0x9c)
122
123 int gdrom_getstat(void);
124 int gdrom_do_command(struct gdrom_softc *, void *, void *, unsigned int,
125 int *);
126 int gdrom_command_sense(struct gdrom_softc *, void *, void *, unsigned int,
127 int *);
128 int gdrom_read_toc(struct gdrom_softc *, struct gd_toc *);
129 int gdrom_read_sectors(struct gdrom_softc *, void *, int, int, int *);
130 int gdrom_mount_disk(struct gdrom_softc *);
131 int gdrom_intr(void *);
132 void gdrom_start(struct gdrom_softc *);
133
134 int gdrom_getstat(void)
135 {
136 int s1, s2, s3;
137
138 if (GDROM_BUSY & 0x80)
139 return -1;
140 s1 = GDROM_STAT;
141 s2 = GDROM_STAT;
142 s3 = GDROM_STAT;
143 if (GDROM_BUSY & 0x80)
144 return -1;
145 if (s1 == s2)
146 return s1;
147 else if (s2 == s3)
148 return s2;
149 else
150 return -1;
151 }
152
153 int
154 gdrom_intr(void *arg)
155 {
156 struct gdrom_softc *sc = arg;
157 int s, cond;
158
159 s = splbio();
160 cond = GDROM_COND;
161 #ifdef GDROMDEBUG
162 printf("GDROM: cond = %x\n", cond);
163 #endif
164 if (!sc->cmd_active) {
165 #ifdef GDROMDEBUG
166 printf("GDROM: inactive IRQ!?\n");
167 #endif
168 splx(s);
169 return 0;
170 }
171
172 if ((cond & 8)) {
173 int cnt = (GDROM_CNTHI << 8) | GDROM_CNTLO;
174 #ifdef GDROMDEBUG
175 printf("GDROM: cnt = %d\n", cnt);
176 #endif
177 sc->cmd_actual += cnt;
178 if (cnt > 0 && sc->cmd_result_size > 0) {
179 int subcnt = (cnt > sc->cmd_result_size ?
180 sc->cmd_result_size : cnt);
181 int16_t *ptr = sc->cmd_result_buf;
182 sc->cmd_result_buf = ((char *)sc->cmd_result_buf) +
183 subcnt;
184 sc->cmd_result_size -= subcnt;
185 cnt -= subcnt;
186 while (subcnt > 0) {
187 *ptr++ = GDROM_DATA;
188 subcnt -= 2;
189 }
190 }
191 while (cnt > 0) {
192 volatile int16_t tmp;
193 tmp = GDROM_DATA;
194 cnt -= 2;
195 }
196 }
197 while (GDROM_BUSY & 0x80);
198
199 if ((cond & 8) == 0) {
200 sc->cmd_cond = cond;
201 sc->cmd_active = 0;
202 wakeup(&sc->cmd_active);
203 }
204
205 splx(s);
206 return 1;
207 }
208
209
210 int gdrom_do_command(struct gdrom_softc *sc, void *req, void *buf,
211 unsigned int nbyt, int *resid)
212 {
213 int i, s;
214 short *ptr = req;
215
216 while (GDROM_BUSY & 0x88)
217 ;
218 if (buf != NULL) {
219 GDROM_CNTLO = nbyt & 0xff;
220 GDROM_CNTHI = (nbyt >> 8) & 0xff;
221 GDROM_REGX = 0;
222 }
223 sc->cmd_result_buf = buf;
224 sc->cmd_result_size = nbyt;
225
226 if (GDSTATSTAT(GDROM_STAT) == 6)
227 return -1;
228
229 GDROM_COND = 0xa0;
230 for (i = 0; i < 64; i++)
231 ;
232 while ((GDROM_BUSY & 0x88) != 8)
233 ;
234
235 s = splbio();
236
237 sc->cmd_actual = 0;
238 sc->cmd_active = 1;
239
240 for (i = 0; i< 6; i++)
241 GDROM_DATA = ptr[i];
242
243 while (sc->cmd_active)
244 tsleep(&sc->cmd_active, PRIBIO, "gdrom", 0);
245
246 splx(s);
247
248 if (resid != NULL)
249 *resid = sc->cmd_result_size;
250
251 return sc->cmd_cond;
252 }
253
254
255 int gdrom_command_sense(struct gdrom_softc *sc, void *req, void *buf,
256 unsigned int nbyt, int *resid)
257 {
258 /* 76543210 76543210
259 0 0x13 -
260 2 - bufsz(hi)
261 4 bufsz(lo) -
262 6 - -
263 8 - -
264 10 - - */
265 unsigned short sense_data[5];
266 unsigned char cmd[12];
267 int sense_key, sense_specific;
268
269 int cond = gdrom_do_command(sc, req, buf, nbyt, resid);
270
271 if (cond < 0) {
272 #ifdef GDROMDEBUG
273 printf("GDROM: not ready (2:58)\n");
274 #endif
275 return EIO;
276 }
277
278 if ((cond & 1) == 0) {
279 #ifdef GDROMDEBUG
280 printf("GDROM: no sense. 0:0\n");
281 #endif
282 return 0;
283 }
284
285 memset(cmd, 0, sizeof(cmd));
286
287 cmd[0] = 0x13;
288 cmd[4] = sizeof(sense_data);
289
290 gdrom_do_command(sc, cmd, sense_data, sizeof(sense_data), NULL);
291
292 sense_key = sense_data[1] & 0xf;
293 sense_specific = sense_data[4];
294 if (sense_key == 11 && sense_specific == 0) {
295 #ifdef GDROMDEBUG
296 printf("GDROM: aborted (ignored). 0:0\n");
297 #endif
298 return 0;
299 }
300
301 #ifdef GDROMDEBUG
302 printf("GDROM: SENSE %d:", sense_key);
303 printf("GDROM: %d\n", sense_specific);
304 #endif
305
306 return sense_key == 0 ? 0 : EIO;
307 }
308
309 int gdrom_read_toc(struct gdrom_softc *sc, struct gd_toc *toc)
310 {
311 /* 76543210 76543210
312 0 0x14 -
313 2 - bufsz(hi)
314 4 bufsz(lo) -
315 6 - -
316 8 - -
317 10 - - */
318 unsigned char cmd[12];
319
320 memset(cmd, 0, sizeof(cmd));
321
322 cmd[0] = 0x14;
323 cmd[3] = sizeof(struct gd_toc) >> 8;
324 cmd[4] = sizeof(struct gd_toc) & 0xff;
325
326 return gdrom_command_sense(sc, cmd, toc, sizeof(struct gd_toc), NULL);
327 }
328
329 int gdrom_read_sectors(struct gdrom_softc *sc, void *buf, int sector, int cnt,
330 int *resid)
331 {
332 /* 76543210 76543210
333 0 0x30 datafmt
334 2 sec(hi) sec(mid)
335 4 sec(lo) -
336 6 - -
337 8 cnt(hi) cnt(mid)
338 10 cnt(lo) - */
339 unsigned char cmd[12];
340
341 memset(cmd, 0, sizeof(cmd));
342
343 cmd[0] = 0x30;
344 cmd[1] = 0x20;
345 cmd[2] = sector>>16;
346 cmd[3] = sector>>8;
347 cmd[4] = sector;
348 cmd[8] = cnt>>16;
349 cmd[9] = cnt>>8;
350 cmd[10] = cnt;
351
352 return gdrom_command_sense(sc, cmd, buf, cnt << 11, resid);
353 }
354
355 int gdrom_mount_disk(struct gdrom_softc *sc)
356 {
357 /* 76543210 76543210
358 0 0x70 -
359 2 0x1f -
360 4 - -
361 6 - -
362 8 - -
363 10 - - */
364 unsigned char cmd[12];
365
366 memset(cmd, 0, sizeof(cmd));
367
368 cmd[0] = 0x70;
369 cmd[1] = 0x1f;
370
371 return gdrom_command_sense(sc, cmd, NULL, 0, NULL);
372 }
373
374 int
375 gdrommatch(device_t parent, cfdata_t cf, void *aux)
376 {
377 static int gdrom_matched = 0;
378
379 /* Allow only once instance. */
380 if (gdrom_matched)
381 return 0;
382 gdrom_matched = 1;
383
384 return 1;
385 }
386
387 void
388 gdromattach(device_t parent, device_t self, void *aux)
389 {
390 struct gdrom_softc *sc;
391 uint32_t p, x;
392
393 sc = device_private(self);
394 sc->sc_dev = self;
395
396 bufq_alloc(&sc->sc_bufq, "disksort", BUFQ_SORT_RAWBLOCK);
397
398 /*
399 * Initialize and attach the disk structure.
400 */
401 disk_init(&sc->sc_dk, device_xname(self), &gdromdkdriver);
402 disk_attach(&sc->sc_dk);
403
404 /*
405 * reenable disabled drive
406 */
407 *((volatile uint32_t *)0xa05f74e4) = 0x1fffff;
408 for (p = 0; p < 0x200000 / 4; p++)
409 x = ((volatile uint32_t *)0xa0000000)[p];
410
411 printf(": %s\n", sysasic_intr_string(SYSASIC_IRL9));
412 sysasic_intr_establish(SYSASIC_EVENT_GDROM, IPL_BIO, SYSASIC_IRL9,
413 gdrom_intr, sc);
414 }
415
416 int
417 gdromopen(dev_t dev, int flags, int devtype, struct lwp *l)
418 {
419 struct gdrom_softc *sc;
420 int s, error, unit, cnt;
421 struct gd_toc toc;
422
423 #ifdef GDROMDEBUG
424 printf("GDROM: open\n");
425 #endif
426
427 unit = DISKUNIT(dev);
428
429 sc = device_lookup_private(&gdrom_cd, unit);
430 if (sc == NULL)
431 return ENXIO;
432
433 if (sc->is_open)
434 return EBUSY;
435
436 s = splbio();
437 while (sc->is_busy)
438 tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
439 sc->is_busy = 1;
440 splx(s);
441
442 for (cnt = 0; cnt < 5; cnt++)
443 if ((error = gdrom_mount_disk(sc)) == 0)
444 break;
445
446 if (!error)
447 error = gdrom_read_toc(sc, &toc);
448
449 sc->is_busy = 0;
450 wakeup(&sc->is_busy);
451
452 if (error)
453 return error;
454
455 sc->is_open = 1;
456 sc->openpart_start = 150;
457
458 #ifdef GDROMDEBUG
459 printf("GDROM: open OK\n");
460 #endif
461 return 0;
462 }
463
464 int
465 gdromclose(dev_t dev, int flags, int devtype, struct lwp *l)
466 {
467 struct gdrom_softc *sc;
468 int unit;
469 #ifdef GDROMDEBUG
470 printf("GDROM: close\n");
471 #endif
472 unit = DISKUNIT(dev);
473 sc = device_lookup_private(&gdrom_cd, unit);
474
475 sc->is_open = 0;
476
477 return 0;
478 }
479
480 void
481 gdromstrategy(struct buf *bp)
482 {
483 struct gdrom_softc *sc;
484 int s, unit;
485 #ifdef GDROMDEBUG
486 printf("GDROM: strategy\n");
487 #endif
488
489 unit = DISKUNIT(bp->b_dev);
490 sc = device_lookup_private(&gdrom_cd, unit);
491
492 if (bp->b_bcount == 0)
493 goto done;
494
495 bp->b_rawblkno = bp->b_blkno / (2048 / DEV_BSIZE) + sc->openpart_start;
496
497 #ifdef GDROMDEBUG
498 printf("GDROM: read_sectors(%p, %d, %ld) [%ld bytes]\n",
499 bp->b_data, bp->b_rawblkno,
500 bp->b_bcount>>11, bp->b_bcount);
501 #endif
502 s = splbio();
503 bufq_put(sc->sc_bufq, bp);
504 splx(s);
505 if (!sc->is_active)
506 gdrom_start(sc);
507 return;
508
509 done:
510 bp->b_resid = bp->b_bcount;
511 biodone(bp);
512 }
513
514 void
515 gdrom_start(struct gdrom_softc *sc)
516 {
517 struct buf *bp;
518 int error, resid, s;
519
520 sc->is_active = true;
521
522 for (;;) {
523 s = splbio();
524 bp = bufq_get(sc->sc_bufq);
525 if (bp == NULL) {
526 splx(s);
527 break;
528 }
529
530 while (sc->is_busy)
531 tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
532 sc->is_busy = 1;
533 disk_busy(&sc->sc_dk);
534 splx(s);
535
536 error = gdrom_read_sectors(sc, bp->b_data, bp->b_rawblkno,
537 bp->b_bcount >> 11, &resid);
538 bp->b_error = error;
539 bp->b_resid = resid;
540 if (error != 0)
541 bp->b_resid = bp->b_bcount;
542
543 sc->is_busy = 0;
544 wakeup(&sc->is_busy);
545
546 s = splbio();
547 disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid,
548 (bp->b_flags & B_READ) != 0);
549 splx(s);
550 biodone(bp);
551 }
552
553 sc->is_active = false;
554 }
555
556 int
557 gdromioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
558 {
559 struct gdrom_softc *sc;
560 int unit, error;
561 #ifdef GDROMDEBUG
562 printf("GDROM: ioctl %lx\n", cmd);
563 #endif
564
565 unit = DISKUNIT(dev);
566 sc = device_lookup_private(&gdrom_cd, unit);
567
568 switch (cmd) {
569 case CDIOREADMSADDR: {
570 int s, track, sessno = *(int *)addr;
571 struct gd_toc toc;
572
573 if (sessno != 0)
574 return EINVAL;
575
576 s = splbio();
577 while (sc->is_busy)
578 tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
579 sc->is_busy = 1;
580 splx(s);
581
582 error = gdrom_read_toc(sc, &toc);
583
584 sc->is_busy = 0;
585 wakeup(&sc->is_busy);
586
587 if (error)
588 return error;
589
590 for (track = TOC_TRACK(toc.last);
591 track >= TOC_TRACK(toc.first);
592 --track)
593 if (TOC_CTRL(toc.entry[track-1]))
594 break;
595
596 if (track < TOC_TRACK(toc.first) || track > 100)
597 return ENXIO;
598
599 *(int *)addr = htonl(TOC_LBA(toc.entry[track-1])) -
600 sc->openpart_start;
601
602 return 0;
603 }
604 default:
605 return ENOTTY;
606 }
607
608 #ifdef DIAGNOSTIC
609 panic("gdromioctl: impossible");
610 #endif
611 }
612
613
614 int
615 gdromread(dev_t dev, struct uio *uio, int flags)
616 {
617 #ifdef GDROMDEBUG
618 printf("GDROM: read\n");
619 #endif
620 return physio(gdromstrategy, NULL, dev, B_READ, minphys, uio);
621 }
622
623 int
624 gdromwrite(dev_t dev, struct uio *uio, int flags)
625 {
626
627 return EROFS;
628 }
629