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