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