patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / scsi / sr_vendor.c
1 /* -*-linux-c-*-
2
3  * vendor-specific code for SCSI CD-ROM's goes here.
4  *
5  * This is needed becauce most of the new features (multisession and
6  * the like) are too new to be included into the SCSI-II standard (to
7  * be exact: there is'nt anything in my draft copy).
8  *
9  * Aug 1997: Ha! Got a SCSI-3 cdrom spec across my fingers. SCSI-3 does
10  *           multisession using the READ TOC command (like SONY).
11  *
12  *           Rearranged stuff here: SCSI-3 is included allways, support
13  *           for NEC/TOSHIBA/HP commands is optional.
14  *
15  *   Gerd Knorr <kraxel@cs.tu-berlin.de> 
16  *
17  * --------------------------------------------------------------------------
18  *
19  * support for XA/multisession-CD's
20  * 
21  *   - NEC:     Detection and support of multisession CD's.
22  *     
23  *   - TOSHIBA: Detection and support of multisession CD's.
24  *              Some XA-Sector tweaking, required for older drives.
25  *
26  *   - SONY:    Detection and support of multisession CD's.
27  *              added by Thomas Quinot <thomas@cuivre.freenix.fr>
28  *
29  *   - PIONEER, HITACHI, PLEXTOR, MATSHITA, TEAC, PHILIPS: known to
30  *              work with SONY (SCSI3 now)  code.
31  *
32  *   - HP:      Much like SONY, but a little different... (Thomas)
33  *              HP-Writers only ??? Maybe other CD-Writers work with this too ?
34  *              HP 6020 writers now supported.
35  */
36
37 #include <linux/config.h>
38 #include <linux/errno.h>
39 #include <linux/string.h>
40 #include <linux/bcd.h>
41
42 #include <linux/blkdev.h>
43 #include "scsi.h"
44 #include "hosts.h"
45 #include <scsi/scsi_ioctl.h>
46
47 #include <linux/cdrom.h>
48 #include "sr.h"
49
50 #if 0
51 #define DEBUG
52 #endif
53
54 /* here are some constants to sort the vendors into groups */
55
56 #define VENDOR_SCSI3           1        /* default: scsi-3 mmc */
57
58 #define VENDOR_NEC             2
59 #define VENDOR_TOSHIBA         3
60 #define VENDOR_WRITER          4        /* pre-scsi3 writers */
61
62 #define VENDOR_TIMEOUT  30*HZ
63
64 void sr_vendor_init(Scsi_CD *cd)
65 {
66 #ifndef CONFIG_BLK_DEV_SR_VENDOR
67         cd->vendor = VENDOR_SCSI3;
68 #else
69         char *vendor = cd->device->vendor;
70         char *model = cd->device->model;
71         
72         /* default */
73         cd->vendor = VENDOR_SCSI3;
74         if (cd->readcd_known)
75                 /* this is true for scsi3/mmc drives - no more checks */
76                 return;
77
78         if (cd->device->type == TYPE_WORM) {
79                 cd->vendor = VENDOR_WRITER;
80
81         } else if (!strncmp(vendor, "NEC", 3)) {
82                 cd->vendor = VENDOR_NEC;
83                 if (!strncmp(model, "CD-ROM DRIVE:25", 15) ||
84                     !strncmp(model, "CD-ROM DRIVE:36", 15) ||
85                     !strncmp(model, "CD-ROM DRIVE:83", 15) ||
86                     !strncmp(model, "CD-ROM DRIVE:84 ", 16)
87 #if 0
88                 /* my NEC 3x returns the read-raw data if a read-raw
89                    is followed by a read for the same sector - aeb */
90                     || !strncmp(model, "CD-ROM DRIVE:500", 16)
91 #endif
92                     )
93                         /* these can't handle multisession, may hang */
94                         cd->cdi.mask |= CDC_MULTI_SESSION;
95
96         } else if (!strncmp(vendor, "TOSHIBA", 7)) {
97                 cd->vendor = VENDOR_TOSHIBA;
98
99         }
100 #endif
101 }
102
103
104 /* small handy function for switching block length using MODE SELECT,
105  * used by sr_read_sector() */
106
107 int sr_set_blocklength(Scsi_CD *cd, int blocklength)
108 {
109         unsigned char *buffer;  /* the buffer for the ioctl */
110         struct packet_command cgc;
111         struct ccs_modesel_head *modesel;
112         int rc, density = 0;
113
114 #ifdef CONFIG_BLK_DEV_SR_VENDOR
115         if (cd->vendor == VENDOR_TOSHIBA)
116                 density = (blocklength > 2048) ? 0x81 : 0x83;
117 #endif
118
119         buffer = (unsigned char *) kmalloc(512, GFP_KERNEL | GFP_DMA);
120         if (!buffer)
121                 return -ENOMEM;
122
123 #ifdef DEBUG
124         printk("%s: MODE SELECT 0x%x/%d\n", cd->cdi.name, density, blocklength);
125 #endif
126         memset(&cgc, 0, sizeof(struct packet_command));
127         cgc.cmd[0] = MODE_SELECT;
128         cgc.cmd[1] = (1 << 4);
129         cgc.cmd[4] = 12;
130         modesel = (struct ccs_modesel_head *) buffer;
131         memset(modesel, 0, sizeof(*modesel));
132         modesel->block_desc_length = 0x08;
133         modesel->density = density;
134         modesel->block_length_med = (blocklength >> 8) & 0xff;
135         modesel->block_length_lo = blocklength & 0xff;
136         cgc.buffer = buffer;
137         cgc.buflen = sizeof(*modesel);
138         cgc.data_direction = SCSI_DATA_WRITE;
139         cgc.timeout = VENDOR_TIMEOUT;
140         if (0 == (rc = sr_do_ioctl(cd, &cgc))) {
141                 cd->device->sector_size = blocklength;
142         }
143 #ifdef DEBUG
144         else
145                 printk("%s: switching blocklength to %d bytes failed\n",
146                        cd->cdi.name, blocklength);
147 #endif
148         kfree(buffer);
149         return rc;
150 }
151
152 /* This function gets called after a media change. Checks if the CD is
153    multisession, asks for offset etc. */
154
155 int sr_cd_check(struct cdrom_device_info *cdi)
156 {
157         Scsi_CD *cd = cdi->handle;
158         unsigned long sector;
159         unsigned char *buffer;  /* the buffer for the ioctl */
160         struct packet_command cgc;
161         int rc, no_multi;
162
163         if (cd->cdi.mask & CDC_MULTI_SESSION)
164                 return 0;
165
166         buffer = (unsigned char *) kmalloc(512, GFP_KERNEL | GFP_DMA);
167         if (!buffer)
168                 return -ENOMEM;
169
170         sector = 0;             /* the multisession sector offset goes here  */
171         no_multi = 0;           /* flag: the drive can't handle multisession */
172         rc = 0;
173
174         memset(&cgc, 0, sizeof(struct packet_command));
175
176         switch (cd->vendor) {
177
178         case VENDOR_SCSI3:
179                 cgc.cmd[0] = READ_TOC;
180                 cgc.cmd[8] = 12;
181                 cgc.cmd[9] = 0x40;
182                 cgc.buffer = buffer;
183                 cgc.buflen = 12;
184                 cgc.quiet = 1;
185                 cgc.data_direction = SCSI_DATA_READ;
186                 cgc.timeout = VENDOR_TIMEOUT;
187                 rc = sr_do_ioctl(cd, &cgc);
188                 if (rc != 0)
189                         break;
190                 if ((buffer[0] << 8) + buffer[1] < 0x0a) {
191                         printk(KERN_INFO "%s: Hmm, seems the drive "
192                            "doesn't support multisession CD's\n", cd->cdi.name);
193                         no_multi = 1;
194                         break;
195                 }
196                 sector = buffer[11] + (buffer[10] << 8) +
197                     (buffer[9] << 16) + (buffer[8] << 24);
198                 if (buffer[6] <= 1) {
199                         /* ignore sector offsets from first track */
200                         sector = 0;
201                 }
202                 break;
203
204 #ifdef CONFIG_BLK_DEV_SR_VENDOR
205         case VENDOR_NEC:{
206                         unsigned long min, sec, frame;
207                         cgc.cmd[0] = 0xde;
208                         cgc.cmd[1] = 0x03;
209                         cgc.cmd[2] = 0xb0;
210                         cgc.buffer = buffer;
211                         cgc.buflen = 0x16;
212                         cgc.quiet = 1;
213                         cgc.data_direction = SCSI_DATA_READ;
214                         cgc.timeout = VENDOR_TIMEOUT;
215                         rc = sr_do_ioctl(cd, &cgc);
216                         if (rc != 0)
217                                 break;
218                         if (buffer[14] != 0 && buffer[14] != 0xb0) {
219                                 printk(KERN_INFO "%s: Hmm, seems the cdrom "
220                                        "doesn't support multisession CD's\n",
221                                        cd->cdi.name);
222                                 no_multi = 1;
223                                 break;
224                         }
225                         min = BCD2BIN(buffer[15]);
226                         sec = BCD2BIN(buffer[16]);
227                         frame = BCD2BIN(buffer[17]);
228                         sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
229                         break;
230                 }
231
232         case VENDOR_TOSHIBA:{
233                         unsigned long min, sec, frame;
234
235                         /* we request some disc information (is it a XA-CD ?,
236                          * where starts the last session ?) */
237                         cgc.cmd[0] = 0xc7;
238                         cgc.cmd[1] = 0x03;
239                         cgc.buffer = buffer;
240                         cgc.buflen = 4;
241                         cgc.quiet = 1;
242                         cgc.data_direction = SCSI_DATA_READ;
243                         cgc.timeout = VENDOR_TIMEOUT;
244                         rc = sr_do_ioctl(cd, &cgc);
245                         if (rc == -EINVAL) {
246                                 printk(KERN_INFO "%s: Hmm, seems the drive "
247                                        "doesn't support multisession CD's\n",
248                                        cd->cdi.name);
249                                 no_multi = 1;
250                                 break;
251                         }
252                         if (rc != 0)
253                                 break;
254                         min = BCD2BIN(buffer[1]);
255                         sec = BCD2BIN(buffer[2]);
256                         frame = BCD2BIN(buffer[3]);
257                         sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
258                         if (sector)
259                                 sector -= CD_MSF_OFFSET;
260                         sr_set_blocklength(cd, 2048);
261                         break;
262                 }
263
264         case VENDOR_WRITER:
265                 cgc.cmd[0] = READ_TOC;
266                 cgc.cmd[8] = 0x04;
267                 cgc.cmd[9] = 0x40;
268                 cgc.buffer = buffer;
269                 cgc.buflen = 0x04;
270                 cgc.quiet = 1;
271                 cgc.data_direction = SCSI_DATA_READ;
272                 cgc.timeout = VENDOR_TIMEOUT;
273                 rc = sr_do_ioctl(cd, &cgc);
274                 if (rc != 0) {
275                         break;
276                 }
277                 if ((rc = buffer[2]) == 0) {
278                         printk(KERN_WARNING
279                                "%s: No finished session\n", cd->cdi.name);
280                         break;
281                 }
282                 cgc.cmd[0] = READ_TOC;  /* Read TOC */
283                 cgc.cmd[6] = rc & 0x7f; /* number of last session */
284                 cgc.cmd[8] = 0x0c;
285                 cgc.cmd[9] = 0x40;
286                 cgc.buffer = buffer;
287                 cgc.buflen = 12;
288                 cgc.quiet = 1;
289                 cgc.data_direction = SCSI_DATA_READ;
290                 cgc.timeout = VENDOR_TIMEOUT;
291                 rc = sr_do_ioctl(cd, &cgc);
292                 if (rc != 0) {
293                         break;
294                 }
295                 sector = buffer[11] + (buffer[10] << 8) +
296                     (buffer[9] << 16) + (buffer[8] << 24);
297                 break;
298 #endif                          /* CONFIG_BLK_DEV_SR_VENDOR */
299
300         default:
301                 /* should not happen */
302                 printk(KERN_WARNING
303                    "%s: unknown vendor code (%i), not initialized ?\n",
304                        cd->cdi.name, cd->vendor);
305                 sector = 0;
306                 no_multi = 1;
307                 break;
308         }
309         cd->ms_offset = sector;
310         cd->xa_flag = 0;
311         if (CDS_AUDIO != sr_disk_status(cdi) && 1 == sr_is_xa(cd))
312                 cd->xa_flag = 1;
313
314         if (2048 != cd->device->sector_size) {
315                 sr_set_blocklength(cd, 2048);
316         }
317         if (no_multi)
318                 cdi->mask |= CDC_MULTI_SESSION;
319
320 #ifdef DEBUG
321         if (sector)
322                 printk(KERN_DEBUG "%s: multisession offset=%lu\n",
323                        cd->cdi.name, sector);
324 #endif
325         kfree(buffer);
326         return rc;
327 }