sr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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 -
  261. blk_rq_pos(SCpnt->request)) << 9;
  262. if (good_bytes < 0 || good_bytes >= this_count)
  263. good_bytes = 0;
  264. /*
  265. * The SCSI specification allows for the value
  266. * returned by READ CAPACITY to be up to 75 2K
  267. * sectors past the last readable block.
  268. * Therefore, if we hit a medium error within the
  269. * last 75 2K sectors, we decrease the saved size
  270. * value.
  271. */
  272. if (error_sector < get_capacity(cd->disk) &&
  273. cd->capacity - error_sector < 4 * 75)
  274. set_capacity(cd->disk, error_sector);
  275. break;
  276. case RECOVERED_ERROR:
  277. good_bytes = this_count;
  278. break;
  279. default:
  280. break;
  281. }
  282. }
  283. return good_bytes;
  284. }
  285. static int sr_prep_fn(struct request_queue *q, struct request *rq)
  286. {
  287. int block = 0, this_count, s_size;
  288. struct scsi_cd *cd;
  289. struct scsi_cmnd *SCpnt;
  290. struct scsi_device *sdp = q->queuedata;
  291. int ret;
  292. if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  293. ret = scsi_setup_blk_pc_cmnd(sdp, rq);
  294. goto out;
  295. } else if (rq->cmd_type != REQ_TYPE_FS) {
  296. ret = BLKPREP_KILL;
  297. goto out;
  298. }
  299. ret = scsi_setup_fs_cmnd(sdp, rq);
  300. if (ret != BLKPREP_OK)
  301. goto out;
  302. SCpnt = rq->special;
  303. cd = scsi_cd(rq->rq_disk);
  304. /* from here on until we're complete, any goto out
  305. * is used for a killable error condition */
  306. ret = BLKPREP_KILL;
  307. SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
  308. cd->disk->disk_name, block));
  309. if (!cd->device || !scsi_device_online(cd->device)) {
  310. SCSI_LOG_HLQUEUE(2, printk("Finishing %u sectors\n",
  311. blk_rq_sectors(rq)));
  312. SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
  313. goto out;
  314. }
  315. if (cd->device->changed) {
  316. /*
  317. * quietly refuse to do anything to a changed disc until the
  318. * changed bit has been reset
  319. */
  320. goto out;
  321. }
  322. /*
  323. * we do lazy blocksize switching (when reading XA sectors,
  324. * see CDROMREADMODE2 ioctl)
  325. */
  326. s_size = cd->device->sector_size;
  327. if (s_size > 2048) {
  328. if (!in_interrupt())
  329. sr_set_blocklength(cd, 2048);
  330. else
  331. printk("sr: can't switch blocksize: in interrupt\n");
  332. }
  333. if (s_size != 512 && s_size != 1024 && s_size != 2048) {
  334. scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
  335. goto out;
  336. }
  337. if (rq_data_dir(rq) == WRITE) {
  338. if (!cd->device->writeable)
  339. goto out;
  340. SCpnt->cmnd[0] = WRITE_10;
  341. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  342. cd->cdi.media_written = 1;
  343. } else if (rq_data_dir(rq) == READ) {
  344. SCpnt->cmnd[0] = READ_10;
  345. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  346. } else {
  347. blk_dump_rq_flags(rq, "Unknown sr command");
  348. goto out;
  349. }
  350. {
  351. struct scatterlist *sg;
  352. int i, size = 0, sg_count = scsi_sg_count(SCpnt);
  353. scsi_for_each_sg(SCpnt, sg, sg_count, i)
  354. size += sg->length;
  355. if (size != scsi_bufflen(SCpnt)) {
  356. scmd_printk(KERN_ERR, SCpnt,
  357. "mismatch count %d, bytes %d\n",
  358. size, scsi_bufflen(SCpnt));
  359. if (scsi_bufflen(SCpnt) > size)
  360. SCpnt->sdb.length = size;
  361. }
  362. }
  363. /*
  364. * request doesn't start on hw block boundary, add scatter pads
  365. */
  366. if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
  367. (scsi_bufflen(SCpnt) % s_size)) {
  368. scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
  369. goto out;
  370. }
  371. this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
  372. SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%u 512 byte blocks.\n",
  373. cd->cdi.name,
  374. (rq_data_dir(rq) == WRITE) ?
  375. "writing" : "reading",
  376. this_count, blk_rq_sectors(rq)));
  377. SCpnt->cmnd[1] = 0;
  378. block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
  379. if (this_count > 0xffff) {
  380. this_count = 0xffff;
  381. SCpnt->sdb.length = this_count * s_size;
  382. }
  383. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  384. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  385. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  386. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  387. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  388. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  389. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  390. /*
  391. * We shouldn't disconnect in the middle of a sector, so with a dumb
  392. * host adapter, it's safe to assume that we can at least transfer
  393. * this many bytes between each connect / disconnect.
  394. */
  395. SCpnt->transfersize = cd->device->sector_size;
  396. SCpnt->underflow = this_count << 9;
  397. SCpnt->allowed = MAX_RETRIES;
  398. /*
  399. * This indicates that the command is ready from our end to be
  400. * queued.
  401. */
  402. ret = BLKPREP_OK;
  403. out:
  404. return scsi_prep_return(q, rq, ret);
  405. }
  406. static int sr_block_open(struct block_device *bdev, fmode_t mode)
  407. {
  408. struct scsi_cd *cd = scsi_cd_get(bdev->bd_disk);
  409. int ret = -ENXIO;
  410. if (cd) {
  411. ret = cdrom_open(&cd->cdi, bdev, mode);
  412. if (ret)
  413. scsi_cd_put(cd);
  414. }
  415. return ret;
  416. }
  417. static int sr_block_release(struct gendisk *disk, fmode_t mode)
  418. {
  419. struct scsi_cd *cd = scsi_cd(disk);
  420. cdrom_release(&cd->cdi, mode);
  421. scsi_cd_put(cd);
  422. return 0;
  423. }
  424. static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
  425. unsigned long arg)
  426. {
  427. struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
  428. struct scsi_device *sdev = cd->device;
  429. void __user *argp = (void __user *)arg;
  430. int ret;
  431. /*
  432. * Send SCSI addressing ioctls directly to mid level, send other
  433. * ioctls to cdrom/block level.
  434. */
  435. switch (cmd) {
  436. case SCSI_IOCTL_GET_IDLUN:
  437. case SCSI_IOCTL_GET_BUS_NUMBER:
  438. return scsi_ioctl(sdev, cmd, argp);
  439. }
  440. ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
  441. if (ret != -ENOSYS)
  442. return ret;
  443. /*
  444. * ENODEV means that we didn't recognise the ioctl, or that we
  445. * cannot execute it in the current device state. In either
  446. * case fall through to scsi_ioctl, which will return ENDOEV again
  447. * if it doesn't recognise the ioctl
  448. */
  449. ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
  450. (mode & FMODE_NDELAY) != 0);
  451. if (ret != -ENODEV)
  452. return ret;
  453. return scsi_ioctl(sdev, cmd, argp);
  454. }
  455. static int sr_block_media_changed(struct gendisk *disk)
  456. {
  457. struct scsi_cd *cd = scsi_cd(disk);
  458. return cdrom_media_changed(&cd->cdi);
  459. }
  460. static struct block_device_operations sr_bdops =
  461. {
  462. .owner = THIS_MODULE,
  463. .open = sr_block_open,
  464. .release = sr_block_release,
  465. .locked_ioctl = sr_block_ioctl,
  466. .media_changed = sr_block_media_changed,
  467. /*
  468. * No compat_ioctl for now because sr_block_ioctl never
  469. * seems to pass arbitary ioctls down to host drivers.
  470. */
  471. };
  472. static int sr_open(struct cdrom_device_info *cdi, int purpose)
  473. {
  474. struct scsi_cd *cd = cdi->handle;
  475. struct scsi_device *sdev = cd->device;
  476. int retval;
  477. /*
  478. * If the device is in error recovery, wait until it is done.
  479. * If the device is offline, then disallow any access to it.
  480. */
  481. retval = -ENXIO;
  482. if (!scsi_block_when_processing_errors(sdev))
  483. goto error_out;
  484. return 0;
  485. error_out:
  486. return retval;
  487. }
  488. static void sr_release(struct cdrom_device_info *cdi)
  489. {
  490. struct scsi_cd *cd = cdi->handle;
  491. if (cd->device->sector_size > 2048)
  492. sr_set_blocklength(cd, 2048);
  493. }
  494. static int sr_probe(struct device *dev)
  495. {
  496. struct scsi_device *sdev = to_scsi_device(dev);
  497. struct gendisk *disk;
  498. struct scsi_cd *cd;
  499. int minor, error;
  500. error = -ENODEV;
  501. if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
  502. goto fail;
  503. error = -ENOMEM;
  504. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  505. if (!cd)
  506. goto fail;
  507. kref_init(&cd->kref);
  508. disk = alloc_disk(1);
  509. if (!disk)
  510. goto fail_free;
  511. spin_lock(&sr_index_lock);
  512. minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
  513. if (minor == SR_DISKS) {
  514. spin_unlock(&sr_index_lock);
  515. error = -EBUSY;
  516. goto fail_put;
  517. }
  518. __set_bit(minor, sr_index_bits);
  519. spin_unlock(&sr_index_lock);
  520. disk->major = SCSI_CDROM_MAJOR;
  521. disk->first_minor = minor;
  522. sprintf(disk->disk_name, "sr%d", minor);
  523. disk->fops = &sr_bdops;
  524. disk->flags = GENHD_FL_CD;
  525. blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
  526. cd->device = sdev;
  527. cd->disk = disk;
  528. cd->driver = &sr_template;
  529. cd->disk = disk;
  530. cd->capacity = 0x1fffff;
  531. cd->device->changed = 1; /* force recheck CD type */
  532. cd->previous_state = 1;
  533. cd->use = 1;
  534. cd->readcd_known = 0;
  535. cd->readcd_cdda = 0;
  536. cd->cdi.ops = &sr_dops;
  537. cd->cdi.handle = cd;
  538. cd->cdi.mask = 0;
  539. cd->cdi.capacity = 1;
  540. sprintf(cd->cdi.name, "sr%d", minor);
  541. sdev->sector_size = 2048; /* A guess, just in case */
  542. /* FIXME: need to handle a get_capabilities failure properly ?? */
  543. get_capabilities(cd);
  544. blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
  545. sr_vendor_init(cd);
  546. disk->driverfs_dev = &sdev->sdev_gendev;
  547. set_capacity(disk, cd->capacity);
  548. disk->private_data = &cd->driver;
  549. disk->queue = sdev->request_queue;
  550. cd->cdi.disk = disk;
  551. if (register_cdrom(&cd->cdi))
  552. goto fail_put;
  553. dev_set_drvdata(dev, cd);
  554. disk->flags |= GENHD_FL_REMOVABLE;
  555. add_disk(disk);
  556. sdev_printk(KERN_DEBUG, sdev,
  557. "Attached scsi CD-ROM %s\n", cd->cdi.name);
  558. return 0;
  559. fail_put:
  560. put_disk(disk);
  561. fail_free:
  562. kfree(cd);
  563. fail:
  564. return error;
  565. }
  566. static void get_sectorsize(struct scsi_cd *cd)
  567. {
  568. unsigned char cmd[10];
  569. unsigned char buffer[8];
  570. int the_result, retries = 3;
  571. int sector_size;
  572. struct request_queue *queue;
  573. do {
  574. cmd[0] = READ_CAPACITY;
  575. memset((void *) &cmd[1], 0, 9);
  576. memset(buffer, 0, sizeof(buffer));
  577. /* Do the command and wait.. */
  578. the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
  579. buffer, sizeof(buffer), NULL,
  580. SR_TIMEOUT, MAX_RETRIES, NULL);
  581. retries--;
  582. } while (the_result && retries);
  583. if (the_result) {
  584. cd->capacity = 0x1fffff;
  585. sector_size = 2048; /* A guess, just in case */
  586. } else {
  587. #if 0
  588. if (cdrom_get_last_written(&cd->cdi,
  589. &cd->capacity))
  590. #endif
  591. cd->capacity = 1 + ((buffer[0] << 24) |
  592. (buffer[1] << 16) |
  593. (buffer[2] << 8) |
  594. buffer[3]);
  595. sector_size = (buffer[4] << 24) |
  596. (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  597. switch (sector_size) {
  598. /*
  599. * HP 4020i CD-Recorder reports 2340 byte sectors
  600. * Philips CD-Writers report 2352 byte sectors
  601. *
  602. * Use 2k sectors for them..
  603. */
  604. case 0:
  605. case 2340:
  606. case 2352:
  607. sector_size = 2048;
  608. /* fall through */
  609. case 2048:
  610. cd->capacity *= 4;
  611. /* fall through */
  612. case 512:
  613. break;
  614. default:
  615. printk("%s: unsupported sector size %d.\n",
  616. cd->cdi.name, sector_size);
  617. cd->capacity = 0;
  618. }
  619. cd->device->sector_size = sector_size;
  620. /*
  621. * Add this so that we have the ability to correctly gauge
  622. * what the device is capable of.
  623. */
  624. set_capacity(cd->disk, cd->capacity);
  625. }
  626. queue = cd->device->request_queue;
  627. blk_queue_logical_block_size(queue, sector_size);
  628. return;
  629. }
  630. static void get_capabilities(struct scsi_cd *cd)
  631. {
  632. unsigned char *buffer;
  633. struct scsi_mode_data data;
  634. struct scsi_sense_hdr sshdr;
  635. int rc, n;
  636. static const char *loadmech[] =
  637. {
  638. "caddy",
  639. "tray",
  640. "pop-up",
  641. "",
  642. "changer",
  643. "cartridge changer",
  644. "",
  645. ""
  646. };
  647. /* allocate transfer buffer */
  648. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  649. if (!buffer) {
  650. printk(KERN_ERR "sr: out of memory.\n");
  651. return;
  652. }
  653. /* eat unit attentions */
  654. sr_test_unit_ready(cd->device, &sshdr);
  655. /* ask for mode page 0x2a */
  656. rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
  657. SR_TIMEOUT, 3, &data, NULL);
  658. if (!scsi_status_is_good(rc)) {
  659. /* failed, drive doesn't have capabilities mode page */
  660. cd->cdi.speed = 1;
  661. cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
  662. CDC_DVD | CDC_DVD_RAM |
  663. CDC_SELECT_DISC | CDC_SELECT_SPEED |
  664. CDC_MRW | CDC_MRW_W | CDC_RAM);
  665. kfree(buffer);
  666. printk("%s: scsi-1 drive\n", cd->cdi.name);
  667. return;
  668. }
  669. n = data.header_length + data.block_descriptor_length;
  670. cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
  671. cd->readcd_known = 1;
  672. cd->readcd_cdda = buffer[n + 5] & 0x01;
  673. /* print some capability bits */
  674. printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
  675. ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
  676. cd->cdi.speed,
  677. buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
  678. buffer[n + 3] & 0x20 ? "dvd-ram " : "",
  679. buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
  680. buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
  681. buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
  682. loadmech[buffer[n + 6] >> 5]);
  683. if ((buffer[n + 6] >> 5) == 0)
  684. /* caddy drives can't close tray... */
  685. cd->cdi.mask |= CDC_CLOSE_TRAY;
  686. if ((buffer[n + 2] & 0x8) == 0)
  687. /* not a DVD drive */
  688. cd->cdi.mask |= CDC_DVD;
  689. if ((buffer[n + 3] & 0x20) == 0)
  690. /* can't write DVD-RAM media */
  691. cd->cdi.mask |= CDC_DVD_RAM;
  692. if ((buffer[n + 3] & 0x10) == 0)
  693. /* can't write DVD-R media */
  694. cd->cdi.mask |= CDC_DVD_R;
  695. if ((buffer[n + 3] & 0x2) == 0)
  696. /* can't write CD-RW media */
  697. cd->cdi.mask |= CDC_CD_RW;
  698. if ((buffer[n + 3] & 0x1) == 0)
  699. /* can't write CD-R media */
  700. cd->cdi.mask |= CDC_CD_R;
  701. if ((buffer[n + 6] & 0x8) == 0)
  702. /* can't eject */
  703. cd->cdi.mask |= CDC_OPEN_TRAY;
  704. if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
  705. (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
  706. cd->cdi.capacity =
  707. cdrom_number_of_slots(&cd->cdi);
  708. if (cd->cdi.capacity <= 1)
  709. /* not a changer */
  710. cd->cdi.mask |= CDC_SELECT_DISC;
  711. /*else I don't think it can close its tray
  712. cd->cdi.mask |= CDC_CLOSE_TRAY; */
  713. /*
  714. * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
  715. */
  716. if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
  717. (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
  718. cd->device->writeable = 1;
  719. }
  720. kfree(buffer);
  721. }
  722. /*
  723. * sr_packet() is the entry point for the generic commands generated
  724. * by the Uniform CD-ROM layer.
  725. */
  726. static int sr_packet(struct cdrom_device_info *cdi,
  727. struct packet_command *cgc)
  728. {
  729. if (cgc->timeout <= 0)
  730. cgc->timeout = IOCTL_TIMEOUT;
  731. sr_do_ioctl(cdi->handle, cgc);
  732. return cgc->stat;
  733. }
  734. /**
  735. * sr_kref_release - Called to free the scsi_cd structure
  736. * @kref: pointer to embedded kref
  737. *
  738. * sr_ref_mutex must be held entering this routine. Because it is
  739. * called on last put, you should always use the scsi_cd_get()
  740. * scsi_cd_put() helpers which manipulate the semaphore directly
  741. * and never do a direct kref_put().
  742. **/
  743. static void sr_kref_release(struct kref *kref)
  744. {
  745. struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
  746. struct gendisk *disk = cd->disk;
  747. spin_lock(&sr_index_lock);
  748. clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
  749. spin_unlock(&sr_index_lock);
  750. unregister_cdrom(&cd->cdi);
  751. disk->private_data = NULL;
  752. put_disk(disk);
  753. kfree(cd);
  754. }
  755. static int sr_remove(struct device *dev)
  756. {
  757. struct scsi_cd *cd = dev_get_drvdata(dev);
  758. blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
  759. del_gendisk(cd->disk);
  760. mutex_lock(&sr_ref_mutex);
  761. kref_put(&cd->kref, sr_kref_release);
  762. mutex_unlock(&sr_ref_mutex);
  763. return 0;
  764. }
  765. static int __init init_sr(void)
  766. {
  767. int rc;
  768. rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
  769. if (rc)
  770. return rc;
  771. rc = scsi_register_driver(&sr_template.gendrv);
  772. if (rc)
  773. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  774. return rc;
  775. }
  776. static void __exit exit_sr(void)
  777. {
  778. scsi_unregister_driver(&sr_template.gendrv);
  779. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  780. }
  781. module_init(init_sr);
  782. module_exit(exit_sr);
  783. MODULE_LICENSE("GPL");