sr.c 26 KB

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