]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * sr.c Copyright (C) 1992 David Giller | |
3 | * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale | |
4 | * | |
5 | * adapted from: | |
6 | * sd.c Copyright (C) 1992 Drew Eckhardt | |
7 | * Linux scsi disk driver by | |
8 | * Drew Eckhardt <drew@colorado.edu> | |
9 | * | |
10 | * Modified by Eric Youngdale ericy@andante.org to | |
11 | * add scatter-gather, multiple outstanding request, and other | |
12 | * enhancements. | |
13 | * | |
14 | * Modified by Eric Youngdale eric@andante.org to support loadable | |
15 | * low-level scsi drivers. | |
16 | * | |
17 | * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to | |
18 | * provide auto-eject. | |
19 | * | |
20 | * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the | |
21 | * generic cdrom interface | |
22 | * | |
23 | * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet() | |
24 | * interface, capabilities probe additions, ioctl cleanups, etc. | |
25 | * | |
26 | * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs | |
27 | * | |
28 | * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM | |
29 | * transparently and lose the GHOST hack | |
30 | * | |
31 | * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br> | |
32 | * check resource allocation in sr_init and some cleanups | |
33 | */ | |
34 | ||
35 | #include <linux/module.h> | |
36 | #include <linux/fs.h> | |
37 | #include <linux/kernel.h> | |
38 | #include <linux/sched.h> | |
39 | #include <linux/mm.h> | |
40 | #include <linux/bio.h> | |
41 | #include <linux/string.h> | |
42 | #include <linux/errno.h> | |
43 | #include <linux/cdrom.h> | |
44 | #include <linux/interrupt.h> | |
45 | #include <linux/init.h> | |
46 | #include <linux/blkdev.h> | |
0b950672 | 47 | #include <linux/mutex.h> |
1da177e4 LT |
48 | #include <asm/uaccess.h> |
49 | ||
50 | #include <scsi/scsi.h> | |
51 | #include <scsi/scsi_dbg.h> | |
52 | #include <scsi/scsi_device.h> | |
53 | #include <scsi/scsi_driver.h> | |
820732b5 | 54 | #include <scsi/scsi_cmnd.h> |
1da177e4 LT |
55 | #include <scsi/scsi_eh.h> |
56 | #include <scsi/scsi_host.h> | |
57 | #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */ | |
1da177e4 LT |
58 | |
59 | #include "scsi_logging.h" | |
60 | #include "sr.h" | |
61 | ||
62 | ||
63 | #define SR_DISKS 256 | |
64 | ||
65 | #define MAX_RETRIES 3 | |
66 | #define SR_TIMEOUT (30 * HZ) | |
67 | #define SR_CAPABILITIES \ | |
68 | (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \ | |
69 | CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \ | |
70 | CDC_PLAY_AUDIO|CDC_RESET|CDC_IOCTLS|CDC_DRIVE_STATUS| \ | |
71 | CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \ | |
72 | CDC_MRW|CDC_MRW_W|CDC_RAM) | |
73 | ||
74 | static int sr_probe(struct device *); | |
75 | static int sr_remove(struct device *); | |
76 | static int sr_init_command(struct scsi_cmnd *); | |
77 | ||
78 | static struct scsi_driver sr_template = { | |
79 | .owner = THIS_MODULE, | |
80 | .gendrv = { | |
81 | .name = "sr", | |
82 | .probe = sr_probe, | |
83 | .remove = sr_remove, | |
84 | }, | |
85 | .init_command = sr_init_command, | |
86 | }; | |
87 | ||
88 | static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG]; | |
89 | static DEFINE_SPINLOCK(sr_index_lock); | |
90 | ||
91 | /* This semaphore is used to mediate the 0->1 reference get in the | |
92 | * face of object destruction (i.e. we can't allow a get on an | |
93 | * object after last put) */ | |
0b950672 | 94 | static DEFINE_MUTEX(sr_ref_mutex); |
1da177e4 LT |
95 | |
96 | static int sr_open(struct cdrom_device_info *, int); | |
97 | static void sr_release(struct cdrom_device_info *); | |
98 | ||
99 | static void get_sectorsize(struct scsi_cd *); | |
100 | static void get_capabilities(struct scsi_cd *); | |
101 | ||
102 | static int sr_media_change(struct cdrom_device_info *, int); | |
103 | static int sr_packet(struct cdrom_device_info *, struct packet_command *); | |
104 | ||
105 | static struct cdrom_device_ops sr_dops = { | |
106 | .open = sr_open, | |
107 | .release = sr_release, | |
108 | .drive_status = sr_drive_status, | |
109 | .media_changed = sr_media_change, | |
110 | .tray_move = sr_tray_move, | |
111 | .lock_door = sr_lock_door, | |
112 | .select_speed = sr_select_speed, | |
113 | .get_last_session = sr_get_last_session, | |
114 | .get_mcn = sr_get_mcn, | |
115 | .reset = sr_reset, | |
116 | .audio_ioctl = sr_audio_ioctl, | |
117 | .dev_ioctl = sr_dev_ioctl, | |
118 | .capability = SR_CAPABILITIES, | |
119 | .generic_packet = sr_packet, | |
120 | }; | |
121 | ||
122 | static void sr_kref_release(struct kref *kref); | |
123 | ||
124 | static inline struct scsi_cd *scsi_cd(struct gendisk *disk) | |
125 | { | |
126 | return container_of(disk->private_data, struct scsi_cd, driver); | |
127 | } | |
128 | ||
129 | /* | |
130 | * The get and put routines for the struct scsi_cd. Note this entity | |
131 | * has a scsi_device pointer and owns a reference to this. | |
132 | */ | |
133 | static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk) | |
134 | { | |
135 | struct scsi_cd *cd = NULL; | |
136 | ||
0b950672 | 137 | mutex_lock(&sr_ref_mutex); |
1da177e4 LT |
138 | if (disk->private_data == NULL) |
139 | goto out; | |
140 | cd = scsi_cd(disk); | |
141 | kref_get(&cd->kref); | |
142 | if (scsi_device_get(cd->device)) | |
143 | goto out_put; | |
144 | goto out; | |
145 | ||
146 | out_put: | |
147 | kref_put(&cd->kref, sr_kref_release); | |
148 | cd = NULL; | |
149 | out: | |
0b950672 | 150 | mutex_unlock(&sr_ref_mutex); |
1da177e4 LT |
151 | return cd; |
152 | } | |
153 | ||
154 | static inline void scsi_cd_put(struct scsi_cd *cd) | |
155 | { | |
156 | struct scsi_device *sdev = cd->device; | |
157 | ||
0b950672 | 158 | mutex_lock(&sr_ref_mutex); |
1da177e4 LT |
159 | kref_put(&cd->kref, sr_kref_release); |
160 | scsi_device_put(sdev); | |
0b950672 | 161 | mutex_unlock(&sr_ref_mutex); |
1da177e4 LT |
162 | } |
163 | ||
164 | /* | |
165 | * This function checks to see if the media has been changed in the | |
166 | * CDROM drive. It is possible that we have already sensed a change, | |
167 | * or the drive may have sensed one and not yet reported it. We must | |
168 | * be ready for either case. This function always reports the current | |
169 | * value of the changed bit. If flag is 0, then the changed bit is reset. | |
170 | * This function could be done as an ioctl, but we would need to have | |
171 | * an inode for that to work, and we do not always have one. | |
172 | */ | |
173 | ||
174 | int sr_media_change(struct cdrom_device_info *cdi, int slot) | |
175 | { | |
176 | struct scsi_cd *cd = cdi->handle; | |
177 | int retval; | |
178 | ||
179 | if (CDSL_CURRENT != slot) { | |
180 | /* no changer support */ | |
181 | return -EINVAL; | |
182 | } | |
183 | ||
184 | retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES); | |
185 | if (retval) { | |
186 | /* Unable to test, unit probably not ready. This usually | |
187 | * means there is no disc in the drive. Mark as changed, | |
188 | * and we will figure it out later once the drive is | |
189 | * available again. */ | |
190 | cd->device->changed = 1; | |
191 | return 1; /* This will force a flush, if called from | |
192 | * check_disk_change */ | |
193 | }; | |
194 | ||
195 | retval = cd->device->changed; | |
196 | cd->device->changed = 0; | |
197 | /* If the disk changed, the capacity will now be different, | |
198 | * so we force a re-read of this information */ | |
199 | if (retval) { | |
200 | /* check multisession offset etc */ | |
201 | sr_cd_check(cdi); | |
202 | ||
51490c89 | 203 | get_sectorsize(cd); |
1da177e4 LT |
204 | } |
205 | return retval; | |
206 | } | |
207 | ||
208 | /* | |
209 | * rw_intr is the interrupt routine for the device driver. | |
210 | * | |
211 | * It will be notified on the end of a SCSI read / write, and will take on | |
212 | * of several actions based on success or failure. | |
213 | */ | |
214 | static void rw_intr(struct scsi_cmnd * SCpnt) | |
215 | { | |
216 | int result = SCpnt->result; | |
217 | int this_count = SCpnt->bufflen; | |
218 | int good_bytes = (result == 0 ? this_count : 0); | |
219 | int block_sectors = 0; | |
220 | long error_sector; | |
221 | struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk); | |
222 | ||
223 | #ifdef DEBUG | |
224 | printk("sr.c done: %x\n", result); | |
225 | #endif | |
226 | ||
227 | /* | |
228 | * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial | |
229 | * success. Since this is a relatively rare error condition, no | |
230 | * care is taken to avoid unnecessary additional work such as | |
231 | * memcpy's that could be avoided. | |
232 | */ | |
233 | if (driver_byte(result) != 0 && /* An error occurred */ | |
234 | (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */ | |
235 | switch (SCpnt->sense_buffer[2]) { | |
236 | case MEDIUM_ERROR: | |
237 | case VOLUME_OVERFLOW: | |
238 | case ILLEGAL_REQUEST: | |
239 | if (!(SCpnt->sense_buffer[0] & 0x90)) | |
240 | break; | |
241 | if (!blk_fs_request(SCpnt->request)) | |
242 | break; | |
243 | error_sector = (SCpnt->sense_buffer[3] << 24) | | |
244 | (SCpnt->sense_buffer[4] << 16) | | |
245 | (SCpnt->sense_buffer[5] << 8) | | |
246 | SCpnt->sense_buffer[6]; | |
247 | if (SCpnt->request->bio != NULL) | |
248 | block_sectors = | |
249 | bio_sectors(SCpnt->request->bio); | |
250 | if (block_sectors < 4) | |
251 | block_sectors = 4; | |
252 | if (cd->device->sector_size == 2048) | |
253 | error_sector <<= 2; | |
254 | error_sector &= ~(block_sectors - 1); | |
255 | good_bytes = (error_sector - SCpnt->request->sector) << 9; | |
256 | if (good_bytes < 0 || good_bytes >= this_count) | |
257 | good_bytes = 0; | |
258 | /* | |
259 | * The SCSI specification allows for the value | |
260 | * returned by READ CAPACITY to be up to 75 2K | |
261 | * sectors past the last readable block. | |
262 | * Therefore, if we hit a medium error within the | |
263 | * last 75 2K sectors, we decrease the saved size | |
264 | * value. | |
265 | */ | |
266 | if (error_sector < get_capacity(cd->disk) && | |
267 | cd->capacity - error_sector < 4 * 75) | |
268 | set_capacity(cd->disk, error_sector); | |
269 | break; | |
270 | ||
271 | case RECOVERED_ERROR: | |
272 | ||
273 | /* | |
274 | * An error occured, but it recovered. Inform the | |
275 | * user, but make sure that it's not treated as a | |
276 | * hard error. | |
277 | */ | |
278 | scsi_print_sense("sr", SCpnt); | |
279 | SCpnt->result = 0; | |
280 | SCpnt->sense_buffer[0] = 0x0; | |
281 | good_bytes = this_count; | |
282 | break; | |
283 | ||
284 | default: | |
285 | break; | |
286 | } | |
287 | } | |
288 | ||
289 | /* | |
290 | * This calls the generic completion function, now that we know | |
291 | * how many actual sectors finished, and how many sectors we need | |
292 | * to say have failed. | |
293 | */ | |
294 | scsi_io_completion(SCpnt, good_bytes, block_sectors << 9); | |
295 | } | |
296 | ||
297 | static int sr_init_command(struct scsi_cmnd * SCpnt) | |
298 | { | |
299 | int block=0, this_count, s_size, timeout = SR_TIMEOUT; | |
300 | struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk); | |
301 | ||
302 | SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n", | |
303 | cd->disk->disk_name, block)); | |
304 | ||
305 | if (!cd->device || !scsi_device_online(cd->device)) { | |
306 | SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", | |
307 | SCpnt->request->nr_sectors)); | |
308 | SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt)); | |
309 | return 0; | |
310 | } | |
311 | ||
312 | if (cd->device->changed) { | |
313 | /* | |
314 | * quietly refuse to do anything to a changed disc until the | |
315 | * changed bit has been reset | |
316 | */ | |
317 | return 0; | |
318 | } | |
319 | ||
320 | /* | |
321 | * these are already setup, just copy cdb basically | |
322 | */ | |
323 | if (SCpnt->request->flags & REQ_BLOCK_PC) { | |
7b16318d | 324 | scsi_setup_blk_pc_cmnd(SCpnt); |
1da177e4 | 325 | |
c9526497 JB |
326 | if (SCpnt->timeout_per_command) |
327 | timeout = SCpnt->timeout_per_command; | |
1da177e4 | 328 | |
1da177e4 LT |
329 | goto queue; |
330 | } | |
331 | ||
332 | if (!(SCpnt->request->flags & REQ_CMD)) { | |
333 | blk_dump_rq_flags(SCpnt->request, "sr unsup command"); | |
334 | return 0; | |
335 | } | |
336 | ||
337 | /* | |
338 | * we do lazy blocksize switching (when reading XA sectors, | |
339 | * see CDROMREADMODE2 ioctl) | |
340 | */ | |
341 | s_size = cd->device->sector_size; | |
342 | if (s_size > 2048) { | |
343 | if (!in_interrupt()) | |
344 | sr_set_blocklength(cd, 2048); | |
345 | else | |
346 | printk("sr: can't switch blocksize: in interrupt\n"); | |
347 | } | |
348 | ||
349 | if (s_size != 512 && s_size != 1024 && s_size != 2048) { | |
3bf743e7 | 350 | scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size); |
1da177e4 LT |
351 | return 0; |
352 | } | |
353 | ||
354 | if (rq_data_dir(SCpnt->request) == WRITE) { | |
355 | if (!cd->device->writeable) | |
356 | return 0; | |
357 | SCpnt->cmnd[0] = WRITE_10; | |
358 | SCpnt->sc_data_direction = DMA_TO_DEVICE; | |
359 | cd->cdi.media_written = 1; | |
360 | } else if (rq_data_dir(SCpnt->request) == READ) { | |
361 | SCpnt->cmnd[0] = READ_10; | |
362 | SCpnt->sc_data_direction = DMA_FROM_DEVICE; | |
363 | } else { | |
364 | blk_dump_rq_flags(SCpnt->request, "Unknown sr command"); | |
365 | return 0; | |
366 | } | |
367 | ||
368 | { | |
369 | struct scatterlist *sg = SCpnt->request_buffer; | |
370 | int i, size = 0; | |
371 | for (i = 0; i < SCpnt->use_sg; i++) | |
372 | size += sg[i].length; | |
373 | ||
374 | if (size != SCpnt->request_bufflen && SCpnt->use_sg) { | |
3bf743e7 JG |
375 | scmd_printk(KERN_ERR, SCpnt, |
376 | "mismatch count %d, bytes %d\n", | |
377 | size, SCpnt->request_bufflen); | |
1da177e4 LT |
378 | if (SCpnt->request_bufflen > size) |
379 | SCpnt->request_bufflen = SCpnt->bufflen = size; | |
380 | } | |
381 | } | |
382 | ||
383 | /* | |
384 | * request doesn't start on hw block boundary, add scatter pads | |
385 | */ | |
386 | if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) || | |
387 | (SCpnt->request_bufflen % s_size)) { | |
3bf743e7 | 388 | scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n"); |
1da177e4 LT |
389 | return 0; |
390 | } | |
391 | ||
392 | this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9); | |
393 | ||
394 | ||
395 | SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n", | |
396 | cd->cdi.name, | |
397 | (rq_data_dir(SCpnt->request) == WRITE) ? | |
398 | "writing" : "reading", | |
399 | this_count, SCpnt->request->nr_sectors)); | |
400 | ||
401 | SCpnt->cmnd[1] = 0; | |
402 | block = (unsigned int)SCpnt->request->sector / (s_size >> 9); | |
403 | ||
404 | if (this_count > 0xffff) { | |
405 | this_count = 0xffff; | |
406 | SCpnt->request_bufflen = SCpnt->bufflen = | |
407 | this_count * s_size; | |
408 | } | |
409 | ||
410 | SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff; | |
411 | SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff; | |
412 | SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff; | |
413 | SCpnt->cmnd[5] = (unsigned char) block & 0xff; | |
414 | SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; | |
415 | SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff; | |
416 | SCpnt->cmnd[8] = (unsigned char) this_count & 0xff; | |
417 | ||
418 | /* | |
419 | * We shouldn't disconnect in the middle of a sector, so with a dumb | |
420 | * host adapter, it's safe to assume that we can at least transfer | |
421 | * this many bytes between each connect / disconnect. | |
422 | */ | |
423 | SCpnt->transfersize = cd->device->sector_size; | |
424 | SCpnt->underflow = this_count << 9; | |
425 | ||
426 | queue: | |
427 | SCpnt->allowed = MAX_RETRIES; | |
428 | SCpnt->timeout_per_command = timeout; | |
429 | ||
430 | /* | |
431 | * This is the completion routine we use. This is matched in terms | |
432 | * of capability to this function. | |
433 | */ | |
434 | SCpnt->done = rw_intr; | |
435 | ||
436 | /* | |
437 | * This indicates that the command is ready from our end to be | |
438 | * queued. | |
439 | */ | |
440 | return 1; | |
441 | } | |
442 | ||
443 | static int sr_block_open(struct inode *inode, struct file *file) | |
444 | { | |
445 | struct gendisk *disk = inode->i_bdev->bd_disk; | |
80d904c4 | 446 | struct scsi_cd *cd; |
1da177e4 LT |
447 | int ret = 0; |
448 | ||
449 | if(!(cd = scsi_cd_get(disk))) | |
450 | return -ENXIO; | |
451 | ||
452 | if((ret = cdrom_open(&cd->cdi, inode, file)) != 0) | |
453 | scsi_cd_put(cd); | |
454 | ||
455 | return ret; | |
456 | } | |
457 | ||
458 | static int sr_block_release(struct inode *inode, struct file *file) | |
459 | { | |
460 | int ret; | |
461 | struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk); | |
462 | ret = cdrom_release(&cd->cdi, file); | |
463 | if(ret) | |
464 | return ret; | |
465 | ||
466 | scsi_cd_put(cd); | |
467 | ||
468 | return 0; | |
469 | } | |
470 | ||
471 | static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd, | |
472 | unsigned long arg) | |
473 | { | |
474 | struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk); | |
475 | struct scsi_device *sdev = cd->device; | |
476 | ||
477 | /* | |
478 | * Send SCSI addressing ioctls directly to mid level, send other | |
479 | * ioctls to cdrom/block level. | |
480 | */ | |
481 | switch (cmd) { | |
482 | case SCSI_IOCTL_GET_IDLUN: | |
483 | case SCSI_IOCTL_GET_BUS_NUMBER: | |
484 | return scsi_ioctl(sdev, cmd, (void __user *)arg); | |
485 | } | |
486 | return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg); | |
487 | } | |
488 | ||
489 | static int sr_block_media_changed(struct gendisk *disk) | |
490 | { | |
491 | struct scsi_cd *cd = scsi_cd(disk); | |
492 | return cdrom_media_changed(&cd->cdi); | |
493 | } | |
494 | ||
495 | static struct block_device_operations sr_bdops = | |
496 | { | |
497 | .owner = THIS_MODULE, | |
498 | .open = sr_block_open, | |
499 | .release = sr_block_release, | |
500 | .ioctl = sr_block_ioctl, | |
501 | .media_changed = sr_block_media_changed, | |
502 | /* | |
503 | * No compat_ioctl for now because sr_block_ioctl never | |
504 | * seems to pass arbitary ioctls down to host drivers. | |
505 | */ | |
506 | }; | |
507 | ||
508 | static int sr_open(struct cdrom_device_info *cdi, int purpose) | |
509 | { | |
510 | struct scsi_cd *cd = cdi->handle; | |
511 | struct scsi_device *sdev = cd->device; | |
512 | int retval; | |
513 | ||
514 | /* | |
515 | * If the device is in error recovery, wait until it is done. | |
516 | * If the device is offline, then disallow any access to it. | |
517 | */ | |
518 | retval = -ENXIO; | |
519 | if (!scsi_block_when_processing_errors(sdev)) | |
520 | goto error_out; | |
521 | ||
1da177e4 LT |
522 | return 0; |
523 | ||
524 | error_out: | |
525 | return retval; | |
526 | } | |
527 | ||
528 | static void sr_release(struct cdrom_device_info *cdi) | |
529 | { | |
530 | struct scsi_cd *cd = cdi->handle; | |
531 | ||
532 | if (cd->device->sector_size > 2048) | |
533 | sr_set_blocklength(cd, 2048); | |
534 | ||
535 | } | |
536 | ||
537 | static int sr_probe(struct device *dev) | |
538 | { | |
539 | struct scsi_device *sdev = to_scsi_device(dev); | |
540 | struct gendisk *disk; | |
541 | struct scsi_cd *cd; | |
542 | int minor, error; | |
543 | ||
544 | error = -ENODEV; | |
545 | if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM) | |
546 | goto fail; | |
547 | ||
548 | error = -ENOMEM; | |
549 | cd = kmalloc(sizeof(*cd), GFP_KERNEL); | |
550 | if (!cd) | |
551 | goto fail; | |
552 | memset(cd, 0, sizeof(*cd)); | |
553 | ||
554 | kref_init(&cd->kref); | |
555 | ||
556 | disk = alloc_disk(1); | |
557 | if (!disk) | |
558 | goto fail_free; | |
559 | ||
560 | spin_lock(&sr_index_lock); | |
561 | minor = find_first_zero_bit(sr_index_bits, SR_DISKS); | |
562 | if (minor == SR_DISKS) { | |
563 | spin_unlock(&sr_index_lock); | |
564 | error = -EBUSY; | |
565 | goto fail_put; | |
566 | } | |
567 | __set_bit(minor, sr_index_bits); | |
568 | spin_unlock(&sr_index_lock); | |
569 | ||
570 | disk->major = SCSI_CDROM_MAJOR; | |
571 | disk->first_minor = minor; | |
572 | sprintf(disk->disk_name, "sr%d", minor); | |
573 | disk->fops = &sr_bdops; | |
574 | disk->flags = GENHD_FL_CD; | |
575 | ||
576 | cd->device = sdev; | |
577 | cd->disk = disk; | |
578 | cd->driver = &sr_template; | |
579 | cd->disk = disk; | |
580 | cd->capacity = 0x1fffff; | |
1da177e4 LT |
581 | cd->device->changed = 1; /* force recheck CD type */ |
582 | cd->use = 1; | |
583 | cd->readcd_known = 0; | |
584 | cd->readcd_cdda = 0; | |
585 | ||
586 | cd->cdi.ops = &sr_dops; | |
587 | cd->cdi.handle = cd; | |
588 | cd->cdi.mask = 0; | |
589 | cd->cdi.capacity = 1; | |
590 | sprintf(cd->cdi.name, "sr%d", minor); | |
591 | ||
592 | sdev->sector_size = 2048; /* A guess, just in case */ | |
593 | ||
594 | /* FIXME: need to handle a get_capabilities failure properly ?? */ | |
595 | get_capabilities(cd); | |
596 | sr_vendor_init(cd); | |
597 | ||
598 | snprintf(disk->devfs_name, sizeof(disk->devfs_name), | |
599 | "%s/cd", sdev->devfs_name); | |
600 | disk->driverfs_dev = &sdev->sdev_gendev; | |
601 | set_capacity(disk, cd->capacity); | |
602 | disk->private_data = &cd->driver; | |
603 | disk->queue = sdev->request_queue; | |
604 | cd->cdi.disk = disk; | |
605 | ||
606 | if (register_cdrom(&cd->cdi)) | |
607 | goto fail_put; | |
608 | ||
609 | dev_set_drvdata(dev, cd); | |
610 | disk->flags |= GENHD_FL_REMOVABLE; | |
611 | add_disk(disk); | |
612 | ||
9ccfc756 JB |
613 | sdev_printk(KERN_DEBUG, sdev, |
614 | "Attached scsi CD-ROM %s\n", cd->cdi.name); | |
1da177e4 LT |
615 | return 0; |
616 | ||
617 | fail_put: | |
618 | put_disk(disk); | |
619 | fail_free: | |
620 | kfree(cd); | |
621 | fail: | |
622 | return error; | |
623 | } | |
624 | ||
625 | ||
626 | static void get_sectorsize(struct scsi_cd *cd) | |
627 | { | |
628 | unsigned char cmd[10]; | |
629 | unsigned char *buffer; | |
630 | int the_result, retries = 3; | |
631 | int sector_size; | |
1da177e4 LT |
632 | request_queue_t *queue; |
633 | ||
634 | buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); | |
635 | if (!buffer) | |
636 | goto Enomem; | |
1da177e4 LT |
637 | |
638 | do { | |
639 | cmd[0] = READ_CAPACITY; | |
640 | memset((void *) &cmd[1], 0, 9); | |
1da177e4 LT |
641 | memset(buffer, 0, 8); |
642 | ||
643 | /* Do the command and wait.. */ | |
820732b5 JB |
644 | the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE, |
645 | buffer, 8, NULL, SR_TIMEOUT, | |
646 | MAX_RETRIES); | |
1da177e4 | 647 | |
1da177e4 LT |
648 | retries--; |
649 | ||
650 | } while (the_result && retries); | |
651 | ||
652 | ||
1da177e4 LT |
653 | if (the_result) { |
654 | cd->capacity = 0x1fffff; | |
655 | sector_size = 2048; /* A guess, just in case */ | |
1da177e4 LT |
656 | } else { |
657 | #if 0 | |
658 | if (cdrom_get_last_written(&cd->cdi, | |
659 | &cd->capacity)) | |
660 | #endif | |
661 | cd->capacity = 1 + ((buffer[0] << 24) | | |
662 | (buffer[1] << 16) | | |
663 | (buffer[2] << 8) | | |
664 | buffer[3]); | |
665 | sector_size = (buffer[4] << 24) | | |
666 | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7]; | |
667 | switch (sector_size) { | |
668 | /* | |
669 | * HP 4020i CD-Recorder reports 2340 byte sectors | |
670 | * Philips CD-Writers report 2352 byte sectors | |
671 | * | |
672 | * Use 2k sectors for them.. | |
673 | */ | |
674 | case 0: | |
675 | case 2340: | |
676 | case 2352: | |
677 | sector_size = 2048; | |
678 | /* fall through */ | |
679 | case 2048: | |
680 | cd->capacity *= 4; | |
681 | /* fall through */ | |
682 | case 512: | |
683 | break; | |
684 | default: | |
685 | printk("%s: unsupported sector size %d.\n", | |
686 | cd->cdi.name, sector_size); | |
687 | cd->capacity = 0; | |
1da177e4 LT |
688 | } |
689 | ||
690 | cd->device->sector_size = sector_size; | |
691 | ||
692 | /* | |
693 | * Add this so that we have the ability to correctly gauge | |
694 | * what the device is capable of. | |
695 | */ | |
1da177e4 LT |
696 | set_capacity(cd->disk, cd->capacity); |
697 | } | |
698 | ||
699 | queue = cd->device->request_queue; | |
700 | blk_queue_hardsect_size(queue, sector_size); | |
701 | out: | |
702 | kfree(buffer); | |
703 | return; | |
704 | ||
705 | Enomem: | |
706 | cd->capacity = 0x1fffff; | |
51490c89 | 707 | cd->device->sector_size = 2048; /* A guess, just in case */ |
1da177e4 LT |
708 | goto out; |
709 | } | |
710 | ||
711 | static void get_capabilities(struct scsi_cd *cd) | |
712 | { | |
713 | unsigned char *buffer; | |
714 | struct scsi_mode_data data; | |
1da177e4 | 715 | unsigned char cmd[MAX_COMMAND_SIZE]; |
820732b5 | 716 | struct scsi_sense_hdr sshdr; |
1da177e4 LT |
717 | unsigned int the_result; |
718 | int retries, rc, n; | |
719 | ||
0ad78200 | 720 | static const char *loadmech[] = |
1da177e4 LT |
721 | { |
722 | "caddy", | |
723 | "tray", | |
724 | "pop-up", | |
725 | "", | |
726 | "changer", | |
727 | "cartridge changer", | |
728 | "", | |
729 | "" | |
730 | }; | |
731 | ||
1da177e4 LT |
732 | |
733 | /* allocate transfer buffer */ | |
734 | buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); | |
735 | if (!buffer) { | |
736 | printk(KERN_ERR "sr: out of memory.\n"); | |
1da177e4 LT |
737 | return; |
738 | } | |
739 | ||
740 | /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION | |
741 | * conditions are gone, or a timeout happens | |
742 | */ | |
743 | retries = 0; | |
744 | do { | |
745 | memset((void *)cmd, 0, MAX_COMMAND_SIZE); | |
746 | cmd[0] = TEST_UNIT_READY; | |
747 | ||
820732b5 JB |
748 | the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL, |
749 | 0, &sshdr, SR_TIMEOUT, | |
750 | MAX_RETRIES); | |
1da177e4 | 751 | |
1da177e4 LT |
752 | retries++; |
753 | } while (retries < 5 && | |
754 | (!scsi_status_is_good(the_result) || | |
820732b5 JB |
755 | (scsi_sense_valid(&sshdr) && |
756 | sshdr.sense_key == UNIT_ATTENTION))); | |
1da177e4 LT |
757 | |
758 | /* ask for mode page 0x2a */ | |
759 | rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128, | |
1cf72699 | 760 | SR_TIMEOUT, 3, &data, NULL); |
1da177e4 LT |
761 | |
762 | if (!scsi_status_is_good(rc)) { | |
763 | /* failed, drive doesn't have capabilities mode page */ | |
764 | cd->cdi.speed = 1; | |
765 | cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R | | |
766 | CDC_DVD | CDC_DVD_RAM | | |
767 | CDC_SELECT_DISC | CDC_SELECT_SPEED); | |
1da177e4 LT |
768 | kfree(buffer); |
769 | printk("%s: scsi-1 drive\n", cd->cdi.name); | |
770 | return; | |
771 | } | |
772 | ||
773 | n = data.header_length + data.block_descriptor_length; | |
774 | cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176; | |
775 | cd->readcd_known = 1; | |
776 | cd->readcd_cdda = buffer[n + 5] & 0x01; | |
777 | /* print some capability bits */ | |
778 | printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name, | |
779 | ((buffer[n + 14] << 8) + buffer[n + 15]) / 176, | |
780 | cd->cdi.speed, | |
781 | buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */ | |
782 | buffer[n + 3] & 0x20 ? "dvd-ram " : "", | |
783 | buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */ | |
784 | buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */ | |
785 | buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */ | |
786 | loadmech[buffer[n + 6] >> 5]); | |
787 | if ((buffer[n + 6] >> 5) == 0) | |
788 | /* caddy drives can't close tray... */ | |
789 | cd->cdi.mask |= CDC_CLOSE_TRAY; | |
790 | if ((buffer[n + 2] & 0x8) == 0) | |
791 | /* not a DVD drive */ | |
792 | cd->cdi.mask |= CDC_DVD; | |
793 | if ((buffer[n + 3] & 0x20) == 0) | |
794 | /* can't write DVD-RAM media */ | |
795 | cd->cdi.mask |= CDC_DVD_RAM; | |
796 | if ((buffer[n + 3] & 0x10) == 0) | |
797 | /* can't write DVD-R media */ | |
798 | cd->cdi.mask |= CDC_DVD_R; | |
799 | if ((buffer[n + 3] & 0x2) == 0) | |
800 | /* can't write CD-RW media */ | |
801 | cd->cdi.mask |= CDC_CD_RW; | |
802 | if ((buffer[n + 3] & 0x1) == 0) | |
803 | /* can't write CD-R media */ | |
804 | cd->cdi.mask |= CDC_CD_R; | |
805 | if ((buffer[n + 6] & 0x8) == 0) | |
806 | /* can't eject */ | |
807 | cd->cdi.mask |= CDC_OPEN_TRAY; | |
808 | ||
809 | if ((buffer[n + 6] >> 5) == mechtype_individual_changer || | |
810 | (buffer[n + 6] >> 5) == mechtype_cartridge_changer) | |
811 | cd->cdi.capacity = | |
812 | cdrom_number_of_slots(&cd->cdi); | |
813 | if (cd->cdi.capacity <= 1) | |
814 | /* not a changer */ | |
815 | cd->cdi.mask |= CDC_SELECT_DISC; | |
816 | /*else I don't think it can close its tray | |
817 | cd->cdi.mask |= CDC_CLOSE_TRAY; */ | |
818 | ||
819 | /* | |
820 | * if DVD-RAM, MRW-W or CD-RW, we are randomly writable | |
821 | */ | |
822 | if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) != | |
823 | (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) { | |
824 | cd->device->writeable = 1; | |
825 | } | |
826 | ||
1da177e4 LT |
827 | kfree(buffer); |
828 | } | |
829 | ||
830 | /* | |
831 | * sr_packet() is the entry point for the generic commands generated | |
832 | * by the Uniform CD-ROM layer. | |
833 | */ | |
834 | static int sr_packet(struct cdrom_device_info *cdi, | |
835 | struct packet_command *cgc) | |
836 | { | |
837 | if (cgc->timeout <= 0) | |
838 | cgc->timeout = IOCTL_TIMEOUT; | |
839 | ||
840 | sr_do_ioctl(cdi->handle, cgc); | |
841 | ||
842 | return cgc->stat; | |
843 | } | |
844 | ||
845 | /** | |
846 | * sr_kref_release - Called to free the scsi_cd structure | |
847 | * @kref: pointer to embedded kref | |
848 | * | |
0b950672 | 849 | * sr_ref_mutex must be held entering this routine. Because it is |
1da177e4 LT |
850 | * called on last put, you should always use the scsi_cd_get() |
851 | * scsi_cd_put() helpers which manipulate the semaphore directly | |
852 | * and never do a direct kref_put(). | |
853 | **/ | |
854 | static void sr_kref_release(struct kref *kref) | |
855 | { | |
856 | struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref); | |
857 | struct gendisk *disk = cd->disk; | |
858 | ||
859 | spin_lock(&sr_index_lock); | |
860 | clear_bit(disk->first_minor, sr_index_bits); | |
861 | spin_unlock(&sr_index_lock); | |
862 | ||
863 | unregister_cdrom(&cd->cdi); | |
864 | ||
865 | disk->private_data = NULL; | |
866 | ||
867 | put_disk(disk); | |
868 | ||
869 | kfree(cd); | |
870 | } | |
871 | ||
872 | static int sr_remove(struct device *dev) | |
873 | { | |
874 | struct scsi_cd *cd = dev_get_drvdata(dev); | |
875 | ||
876 | del_gendisk(cd->disk); | |
877 | ||
0b950672 | 878 | mutex_lock(&sr_ref_mutex); |
1da177e4 | 879 | kref_put(&cd->kref, sr_kref_release); |
0b950672 | 880 | mutex_unlock(&sr_ref_mutex); |
1da177e4 LT |
881 | |
882 | return 0; | |
883 | } | |
884 | ||
885 | static int __init init_sr(void) | |
886 | { | |
887 | int rc; | |
888 | ||
889 | rc = register_blkdev(SCSI_CDROM_MAJOR, "sr"); | |
890 | if (rc) | |
891 | return rc; | |
892 | return scsi_register_driver(&sr_template.gendrv); | |
893 | } | |
894 | ||
895 | static void __exit exit_sr(void) | |
896 | { | |
897 | scsi_unregister_driver(&sr_template.gendrv); | |
898 | unregister_blkdev(SCSI_CDROM_MAJOR, "sr"); | |
899 | } | |
900 | ||
901 | module_init(init_sr); | |
902 | module_exit(exit_sr); | |
903 | MODULE_LICENSE("GPL"); |