sr.c 23 KB

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