sr.c 23 KB

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