1 /*  cdrdao - write audio CD-Rs in disc-at-once mode
2  *
3  *  Copyright (C) 1999  Andreas Mueller <mueller@daneb.ping.de>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /*
20  * $Log: ScsiIf-common.cc,v $
21  * Revision 1.4  2007/12/29 12:26:33  poolshark
22  * Complete rewrite of native Linux SG driver for SG 3.0 using SG_IO ioctl. Code cleanup
23  *
24  * Revision 1.3  2004/04/13 01:23:44  poolshark
25  * Cleanup of scglib selection. Fixed without-scglib option. Default build of scsilib was problematic on older systems
26  *
27  * Revision 1.2  2004/03/23 18:46:07  poolshark
28  * MMC autodetect mode
29  *
30  * Revision 1.1.1.1  2000/02/05 01:36:55  llanero
31  * Uploaded cdrdao 1.1.3 with pre10 patch applied.
32  *
33  */
34 
35 // checks if unit is ready
36 // return: 0: OK, ready
37 //         1: not ready (busy)
38 //         2: not ready, no disk in drive
39 //         3: scsi command failed
40 
41 
42 
testUnitReady()43 int ScsiIf::testUnitReady()
44 {
45   unsigned char cmd[6];
46   const unsigned char *senseData;
47   int senseLen;
48   int ret = 0;
49 
50   memset(cmd, 0, 6);
51 
52   switch (sendCmd(cmd, 6, NULL, 0, NULL, 0, 0)) {
53   case 1:
54     ret = 3;
55     break;
56 
57   case 2:
58     senseData = getSense(senseLen);
59 
60     switch (senseData[2] & 0x0f) {
61     case 0x02: // Not ready
62       switch (senseData[12]) {
63       case 0x3a: // medium not present
64 	ret = 2;
65 	break;
66 
67       default:
68 	ret = 1;
69 	break;
70       }
71       break;
72 
73     case 0x06: // Unit attention
74       ret = 0;
75       break;
76 
77     default:
78       ret = 3;
79       break;
80     }
81   }
82 
83   return ret;
84 }
85 
86 typedef struct {
87     unsigned char p_len;
88     unsigned cd_r_read : 1;
89     unsigned cd_rw_read : 1;
90     unsigned method2  : 1;
91     unsigned dvd_rom_read : 1;
92     unsigned dvd_r_read : 1;
93     unsigned dvd_ram_read : 1;
94     unsigned res_2_67 : 2;
95     unsigned cd_r_write : 1;
96     unsigned cd_rw_write : 1;
97     unsigned test_write : 1;
98     unsigned res_3_3  : 1;
99     unsigned dvd_r_write : 1;
100     unsigned dvd_ram_write : 1;
101     unsigned res_3_67 : 2;
102 } cd_page_2a;
103 
checkMmc(bool * cd_r_read,bool * cd_r_write,bool * cd_rw_read,bool * cd_rw_write)104 bool ScsiIf::checkMmc(bool *cd_r_read,  bool *cd_r_write,
105                       bool *cd_rw_read, bool *cd_rw_write)
106 {
107     static const int MODE_SENSE_G1_CMD = 0x5a;
108     static const int MODE_MAX_SIZE = 256;
109     static const int MODE_PAGE_HEADER_SIZE = 8;
110     static const int MODE_CD_CAP_PAGE = 0x2a;
111 
112     unsigned char mode[MODE_MAX_SIZE];
113     memset(mode, 0, sizeof(mode));
114 
115     // First, read header of mode page 0x2A, to figure out its exact
116     // length. For this, we issue a MODE_SENSE (10) command with a
117     // data length of 8, just the size of the mode header.
118     unsigned char cmd[10];
119     memset(&cmd, 0, sizeof(cmd));
120     cmd[0] = MODE_SENSE_G1_CMD; // MODE SENSE(10)
121     cmd[2] = MODE_CD_CAP_PAGE;
122     cmd[8] = MODE_PAGE_HEADER_SIZE;
123     if (sendCmd((unsigned char*)&cmd, 10, NULL, 0, mode,
124 		MODE_PAGE_HEADER_SIZE) != 0) {
125 	return false;
126     }
127 
128     int len = ((mode[0] << 8) + mode[1]) + 2; // +2 is for address field
129     if (len > MODE_MAX_SIZE) len = MODE_MAX_SIZE;
130 
131     // Now we have the length of page 0x2a, read the whole page.
132     memset(mode, 0, MODE_PAGE_HEADER_SIZE);
133     memset(&cmd, 0, sizeof(cmd));
134     cmd[0] = MODE_SENSE_G1_CMD; // MODE SENSE(10)
135     cmd[2] = MODE_CD_CAP_PAGE;
136     cmd[8] = len;
137     if (sendCmd((unsigned char*)&cmd, 10, NULL, 0, mode, len) != 0) {
138 	return false;
139     }
140 
141   cd_page_2a *p2a = (cd_page_2a*)(mode + 9);
142 
143   *cd_r_read   = p2a->cd_r_read;
144   *cd_r_write  = p2a->cd_r_write;
145   *cd_rw_read  = p2a->cd_rw_read;
146   *cd_rw_write = p2a->cd_rw_write;
147   return true;
148 }
149