From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10b612,7b01843330debb4b,start X-Google-Attributes: gid10b612,public X-Google-Thread: 1164f8,7b01843330debb4b,start X-Google-Attributes: gid1164f8,public From: mmoretti@mordent.com (Mike Moretti) Subject: Problems with cam and scsi io to cd-rom drive... Date: 1995/06/29 Message-ID: <3ssvdq$5tj@sundog.tiac.net> X-Deja-AN: 105260549 distribution: world sender: mike@mordent.com (Mike Moretti) organization: Mordent Software reply-to: mmoretti@mordent.com newsgroups: comp.unix.ultrix,comp.sys.dec Hi, I wrote a small program that uses cam to try to get sense mode data and also do a mode select on my cd-rom drive. The sense mode works great and returns the data I'd expect, however, the mode select always returns with a random I/O error on the drive (when I look at the Request Sense data it always points to a different fp). I can't figure out what's wrong... I'm enclosing the program here (it's pretty small...). Any help would be greatly appreciated. Thanx in advance! Here's the code: /* Some of this code was taken from the XMcd program */ #include #include #include #include #include #include #include #include #include #include #include static int bus = -1, target = -1, lun = -1; #define OP_MODE_SELECT 0x15 #define OP_MODE_SENSE 0x1a #define OP_REQUEST_SENSE 0x03 int my_scsi_cmd(int cdfd, u_char opcode, u_char *buf, int length, u_char pgcode, int rdwr) { UAGT_CAM_CCB uagt; CCB_SCSIIO ccb; u_char *cdb; int status; memset(&uagt, 0, sizeof(uagt)); memset(&ccb, 0, sizeof(ccb)); cdb = (u_char *) ccb.cam_cdb_io.cam_cdb_bytes; cdb[0] = opcode; cdb[1] = (opcode == 0x15) ? 0x10 : 0; cdb[2] = pgcode; cdb[3] = 0; cdb[4] = length & 0xff; cdb[5] = 0; ccb.cam_cdb_len = 6; /* Setup the user agent ccb */ uagt.uagt_ccb = (CCB_HEADER *) &ccb; uagt.uagt_ccblen = sizeof(CCB_SCSIIO); /* Setup the scsi ccb */ ccb.cam_ch.my_addr = (CCB_HEADER *) &ccb; ccb.cam_ch.cam_ccb_len = sizeof(CCB_SCSIIO); ccb.cam_ch.cam_func_code = XPT_SCSI_IO; uagt.uagt_buffer = buf; uagt.uagt_buflen = length; if (rdwr) ccb.cam_ch.cam_flags |= CAM_DIR_OUT; else ccb.cam_ch.cam_flags |= CAM_DIR_IN; ccb.cam_ch.cam_flags |= CAM_DIS_AUTOSENSE; ccb.cam_data_ptr = uagt.uagt_buffer; ccb.cam_dxfer_len = uagt.uagt_buflen; ccb.cam_timeout = 5; ccb.cam_ch.cam_path_id = bus; ccb.cam_ch.cam_target_id = target; ccb.cam_ch.cam_target_lun = lun; if ((status = ioctl(cdfd, UAGT_CAM_IO, ((caddr_t) &uagt))) < 0) { perror("UAGT_CAM_IO ioctl failed"); return status; } /* Check return status */ if ((ccb.cam_ch.cam_status & CAM_STATUS_MASK) != CAM_REQ_CMP) { if (ccb.cam_ch.cam_status & CAM_SIM_QFRZN) { memset(&ccb, 0, sizeof(ccb)); memset(&uagt, 0, sizeof(uagt)); /* Setup the user agent ccb */ uagt.uagt_ccb = (CCB_HEADER *) &ccb; uagt.uagt_ccblen = sizeof(CCB_RELSIM); /* Setup the scsi ccb */ ccb.cam_ch.my_addr = (struct ccb_header *) &ccb; ccb.cam_ch.cam_ccb_len = sizeof(CCB_RELSIM); ccb.cam_ch.cam_func_code = XPT_REL_SIMQ; ccb.cam_ch.cam_path_id = bus; ccb.cam_ch.cam_target_id = target; ccb.cam_ch.cam_target_lun = lun; if ((status = ioctl(cdfd, UAGT_CAM_IO, (caddr_t) &uagt)) < 0) { return status; } fprintf(stderr,"SCSI cmd fault, status: %d\n", ccb.cam_scsi_status); return(-1); } } return(0); } static int my_cam_open(char *cdfn) { struct stat stbuf; int status; int cdfd; if ((status = stat(cdfn, &stbuf)) < 0) { fprintf(stderr,"can't stat %s", cdfn); perror(""); return status; } if (!S_ISCHR(stbuf.st_mode)) { fprintf(stderr, "%s is not a character special device", cdfn); return(-1); } if ((cdfd = open(cdfn, O_RDONLY | O_NDELAY, 0)) >= 0) { struct devget devget; if (ioctl(cdfd, DEVIOCGET, &devget) >= 0) { lun = 0; target = devget.slave_num % 8; devget.slave_num /= 8; bus = devget.slave_num % 8; close(cdfd); if ((cdfd = open("/dev/cam", O_RDWR, 0)) >= 0 || (cdfd = open("/dev/cam", O_RDONLY, 0)) >= 0) { return(cdfd); } } } cdfd = bus = target = lun = -1; return(-1); } static void do_sense(int cdfd, struct mode_sel_sns_data *msd) { int status; memset(msd, 0, sizeof(struct mode_sel_sns_data)); if ((status = my_scsi_cmd(cdfd, OP_MODE_SENSE, (u_char *)msd, 12, 0, 0)) < 0) { fprintf(stderr,"error in sense, %d\n", status); close(cdfd); exit(1); } printf("msd:\n"); printf("\tdenscode: %02x\n", msd->ms_desc.density_code); printf("\tblklen: %02x%02x%02x\n", msd->ms_desc.blklen2, msd->ms_desc.blklen1, msd->ms_desc.blklen0 ); } static void get_error(int cdfd) { int status; struct extended_sense errbuf; memset(&errbuf, 0, sizeof(errbuf)); if ((status = my_scsi_cmd(cdfd, OP_REQUEST_SENSE, (u_char *)&errbuf, 18, 0, 0)) < 0) fprintf(stderr,"error getting error status, %d\n", status); else { fprintf(stderr,"Sense data:\n"); fprintf(stderr,"\tcode: %x%x\n", errbuf.errclass, errbuf.errcode); fprintf(stderr,"\tseg: %d, snsky: %d, ili: %d\n", errbuf.segnum, errbuf.snskey, errbuf.ili); fprintf(stderr,"\tinfo: %02x%02x%02x%02x\n", errbuf.infobyte3, errbuf.infobyte2, errbuf.infobyte1, errbuf.infobyte0); fprintf(stderr,"\tasc: %x, ascq: %x\n\tsks: %d %c\n\tbpv: %d bp: %x fp: %02x%02x\n", errbuf.asc, errbuf.rb2, errbuf.bp >> 7, ((errbuf.bp >> 6) & 1) ? 'c' : 'd', (errbuf.bp >> 3) & 1, errbuf.bp & 0x7, errbuf.fp1, errbuf.fp0); fprintf(stderr,"\n\n"); } } main(int argc, char *argv[]) { int cdfd = my_cam_open(argv[1]); struct mode_sel_sns_data msd; struct page_20_header *ph = (struct page_20_header *)msd.ms_pages; int status; if (cdfd < 0) { fprintf(stderr,"Can't open cd-rom drive\n"); exit(1); } do_sense(cdfd, &msd); /* 1st byte is reserved so zero it */ msd.ms_hdr.sense_len = 0; /* This is really unnecessary since it's already 8 from the sense mode */ msd.ms_hdr.blk_des_len = 8; /* Everything else in the data buffer is not touched (left as what was * returned from the sense). */ if ((status = my_scsi_cmd(cdfd, OP_MODE_SELECT, (u_char *)&msd, 12, 0, 1)) < 0) { fprintf(stderr,"error in select, %d\n", status); get_error(cdfd); } do_sense(cdfd, &msd); close(cdfd); exit(0); }