sr.c 23 KB

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