sr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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;
  411. int ret = -ENXIO;
  412. lock_kernel();
  413. cd = scsi_cd_get(bdev->bd_disk);
  414. if (cd) {
  415. ret = cdrom_open(&cd->cdi, bdev, mode);
  416. if (ret)
  417. scsi_cd_put(cd);
  418. }
  419. unlock_kernel();
  420. return ret;
  421. }
  422. static int sr_block_release(struct gendisk *disk, fmode_t mode)
  423. {
  424. struct scsi_cd *cd = scsi_cd(disk);
  425. lock_kernel();
  426. cdrom_release(&cd->cdi, mode);
  427. scsi_cd_put(cd);
  428. unlock_kernel();
  429. return 0;
  430. }
  431. static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
  432. unsigned long arg)
  433. {
  434. struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
  435. struct scsi_device *sdev = cd->device;
  436. void __user *argp = (void __user *)arg;
  437. int ret;
  438. lock_kernel();
  439. /*
  440. * Send SCSI addressing ioctls directly to mid level, send other
  441. * ioctls to cdrom/block level.
  442. */
  443. switch (cmd) {
  444. case SCSI_IOCTL_GET_IDLUN:
  445. case SCSI_IOCTL_GET_BUS_NUMBER:
  446. ret = scsi_ioctl(sdev, cmd, argp);
  447. goto out;
  448. }
  449. ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
  450. if (ret != -ENOSYS)
  451. goto out;
  452. /*
  453. * ENODEV means that we didn't recognise the ioctl, or that we
  454. * cannot execute it in the current device state. In either
  455. * case fall through to scsi_ioctl, which will return ENDOEV again
  456. * if it doesn't recognise the ioctl
  457. */
  458. ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
  459. (mode & FMODE_NDELAY) != 0);
  460. if (ret != -ENODEV)
  461. goto out;
  462. ret = scsi_ioctl(sdev, cmd, argp);
  463. out:
  464. unlock_kernel();
  465. return ret;
  466. }
  467. static int sr_block_media_changed(struct gendisk *disk)
  468. {
  469. struct scsi_cd *cd = scsi_cd(disk);
  470. return cdrom_media_changed(&cd->cdi);
  471. }
  472. static const struct block_device_operations sr_bdops =
  473. {
  474. .owner = THIS_MODULE,
  475. .open = sr_block_open,
  476. .release = sr_block_release,
  477. .ioctl = sr_block_ioctl,
  478. .media_changed = sr_block_media_changed,
  479. /*
  480. * No compat_ioctl for now because sr_block_ioctl never
  481. * seems to pass arbitary ioctls down to host drivers.
  482. */
  483. };
  484. static int sr_open(struct cdrom_device_info *cdi, int purpose)
  485. {
  486. struct scsi_cd *cd = cdi->handle;
  487. struct scsi_device *sdev = cd->device;
  488. int retval;
  489. /*
  490. * If the device is in error recovery, wait until it is done.
  491. * If the device is offline, then disallow any access to it.
  492. */
  493. retval = -ENXIO;
  494. if (!scsi_block_when_processing_errors(sdev))
  495. goto error_out;
  496. return 0;
  497. error_out:
  498. return retval;
  499. }
  500. static void sr_release(struct cdrom_device_info *cdi)
  501. {
  502. struct scsi_cd *cd = cdi->handle;
  503. if (cd->device->sector_size > 2048)
  504. sr_set_blocklength(cd, 2048);
  505. }
  506. static int sr_probe(struct device *dev)
  507. {
  508. struct scsi_device *sdev = to_scsi_device(dev);
  509. struct gendisk *disk;
  510. struct scsi_cd *cd;
  511. int minor, error;
  512. error = -ENODEV;
  513. if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
  514. goto fail;
  515. error = -ENOMEM;
  516. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  517. if (!cd)
  518. goto fail;
  519. kref_init(&cd->kref);
  520. disk = alloc_disk(1);
  521. if (!disk)
  522. goto fail_free;
  523. spin_lock(&sr_index_lock);
  524. minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
  525. if (minor == SR_DISKS) {
  526. spin_unlock(&sr_index_lock);
  527. error = -EBUSY;
  528. goto fail_put;
  529. }
  530. __set_bit(minor, sr_index_bits);
  531. spin_unlock(&sr_index_lock);
  532. disk->major = SCSI_CDROM_MAJOR;
  533. disk->first_minor = minor;
  534. sprintf(disk->disk_name, "sr%d", minor);
  535. disk->fops = &sr_bdops;
  536. disk->flags = GENHD_FL_CD;
  537. blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
  538. cd->device = sdev;
  539. cd->disk = disk;
  540. cd->driver = &sr_template;
  541. cd->disk = disk;
  542. cd->capacity = 0x1fffff;
  543. cd->device->changed = 1; /* force recheck CD type */
  544. cd->previous_state = 1;
  545. cd->use = 1;
  546. cd->readcd_known = 0;
  547. cd->readcd_cdda = 0;
  548. cd->cdi.ops = &sr_dops;
  549. cd->cdi.handle = cd;
  550. cd->cdi.mask = 0;
  551. cd->cdi.capacity = 1;
  552. sprintf(cd->cdi.name, "sr%d", minor);
  553. sdev->sector_size = 2048; /* A guess, just in case */
  554. /* FIXME: need to handle a get_capabilities failure properly ?? */
  555. get_capabilities(cd);
  556. blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
  557. sr_vendor_init(cd);
  558. disk->driverfs_dev = &sdev->sdev_gendev;
  559. set_capacity(disk, cd->capacity);
  560. disk->private_data = &cd->driver;
  561. disk->queue = sdev->request_queue;
  562. cd->cdi.disk = disk;
  563. if (register_cdrom(&cd->cdi))
  564. goto fail_put;
  565. dev_set_drvdata(dev, cd);
  566. disk->flags |= GENHD_FL_REMOVABLE;
  567. add_disk(disk);
  568. sdev_printk(KERN_DEBUG, sdev,
  569. "Attached scsi CD-ROM %s\n", cd->cdi.name);
  570. return 0;
  571. fail_put:
  572. put_disk(disk);
  573. fail_free:
  574. kfree(cd);
  575. fail:
  576. return error;
  577. }
  578. static void get_sectorsize(struct scsi_cd *cd)
  579. {
  580. unsigned char cmd[10];
  581. unsigned char buffer[8];
  582. int the_result, retries = 3;
  583. int sector_size;
  584. struct request_queue *queue;
  585. do {
  586. cmd[0] = READ_CAPACITY;
  587. memset((void *) &cmd[1], 0, 9);
  588. memset(buffer, 0, sizeof(buffer));
  589. /* Do the command and wait.. */
  590. the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
  591. buffer, sizeof(buffer), NULL,
  592. SR_TIMEOUT, MAX_RETRIES, NULL);
  593. retries--;
  594. } while (the_result && retries);
  595. if (the_result) {
  596. cd->capacity = 0x1fffff;
  597. sector_size = 2048; /* A guess, just in case */
  598. } else {
  599. long last_written;
  600. cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
  601. (buffer[2] << 8) | buffer[3]);
  602. /*
  603. * READ_CAPACITY doesn't return the correct size on
  604. * certain UDF media. If last_written is larger, use
  605. * it instead.
  606. *
  607. * http://bugzilla.kernel.org/show_bug.cgi?id=9668
  608. */
  609. if (!cdrom_get_last_written(&cd->cdi, &last_written))
  610. cd->capacity = max_t(long, cd->capacity, last_written);
  611. sector_size = (buffer[4] << 24) |
  612. (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  613. switch (sector_size) {
  614. /*
  615. * HP 4020i CD-Recorder reports 2340 byte sectors
  616. * Philips CD-Writers report 2352 byte sectors
  617. *
  618. * Use 2k sectors for them..
  619. */
  620. case 0:
  621. case 2340:
  622. case 2352:
  623. sector_size = 2048;
  624. /* fall through */
  625. case 2048:
  626. cd->capacity *= 4;
  627. /* fall through */
  628. case 512:
  629. break;
  630. default:
  631. printk("%s: unsupported sector size %d.\n",
  632. cd->cdi.name, sector_size);
  633. cd->capacity = 0;
  634. }
  635. cd->device->sector_size = sector_size;
  636. /*
  637. * Add this so that we have the ability to correctly gauge
  638. * what the device is capable of.
  639. */
  640. set_capacity(cd->disk, cd->capacity);
  641. }
  642. queue = cd->device->request_queue;
  643. blk_queue_logical_block_size(queue, sector_size);
  644. return;
  645. }
  646. static void get_capabilities(struct scsi_cd *cd)
  647. {
  648. unsigned char *buffer;
  649. struct scsi_mode_data data;
  650. struct scsi_sense_hdr sshdr;
  651. int rc, n;
  652. static const char *loadmech[] =
  653. {
  654. "caddy",
  655. "tray",
  656. "pop-up",
  657. "",
  658. "changer",
  659. "cartridge changer",
  660. "",
  661. ""
  662. };
  663. /* allocate transfer buffer */
  664. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  665. if (!buffer) {
  666. printk(KERN_ERR "sr: out of memory.\n");
  667. return;
  668. }
  669. /* eat unit attentions */
  670. sr_test_unit_ready(cd->device, &sshdr);
  671. /* ask for mode page 0x2a */
  672. rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
  673. SR_TIMEOUT, 3, &data, NULL);
  674. if (!scsi_status_is_good(rc)) {
  675. /* failed, drive doesn't have capabilities mode page */
  676. cd->cdi.speed = 1;
  677. cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
  678. CDC_DVD | CDC_DVD_RAM |
  679. CDC_SELECT_DISC | CDC_SELECT_SPEED |
  680. CDC_MRW | CDC_MRW_W | CDC_RAM);
  681. kfree(buffer);
  682. printk("%s: scsi-1 drive\n", cd->cdi.name);
  683. return;
  684. }
  685. n = data.header_length + data.block_descriptor_length;
  686. cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
  687. cd->readcd_known = 1;
  688. cd->readcd_cdda = buffer[n + 5] & 0x01;
  689. /* print some capability bits */
  690. printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
  691. ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
  692. cd->cdi.speed,
  693. buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
  694. buffer[n + 3] & 0x20 ? "dvd-ram " : "",
  695. buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
  696. buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
  697. buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
  698. loadmech[buffer[n + 6] >> 5]);
  699. if ((buffer[n + 6] >> 5) == 0)
  700. /* caddy drives can't close tray... */
  701. cd->cdi.mask |= CDC_CLOSE_TRAY;
  702. if ((buffer[n + 2] & 0x8) == 0)
  703. /* not a DVD drive */
  704. cd->cdi.mask |= CDC_DVD;
  705. if ((buffer[n + 3] & 0x20) == 0)
  706. /* can't write DVD-RAM media */
  707. cd->cdi.mask |= CDC_DVD_RAM;
  708. if ((buffer[n + 3] & 0x10) == 0)
  709. /* can't write DVD-R media */
  710. cd->cdi.mask |= CDC_DVD_R;
  711. if ((buffer[n + 3] & 0x2) == 0)
  712. /* can't write CD-RW media */
  713. cd->cdi.mask |= CDC_CD_RW;
  714. if ((buffer[n + 3] & 0x1) == 0)
  715. /* can't write CD-R media */
  716. cd->cdi.mask |= CDC_CD_R;
  717. if ((buffer[n + 6] & 0x8) == 0)
  718. /* can't eject */
  719. cd->cdi.mask |= CDC_OPEN_TRAY;
  720. if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
  721. (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
  722. cd->cdi.capacity =
  723. cdrom_number_of_slots(&cd->cdi);
  724. if (cd->cdi.capacity <= 1)
  725. /* not a changer */
  726. cd->cdi.mask |= CDC_SELECT_DISC;
  727. /*else I don't think it can close its tray
  728. cd->cdi.mask |= CDC_CLOSE_TRAY; */
  729. /*
  730. * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
  731. */
  732. if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
  733. (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
  734. cd->device->writeable = 1;
  735. }
  736. kfree(buffer);
  737. }
  738. /*
  739. * sr_packet() is the entry point for the generic commands generated
  740. * by the Uniform CD-ROM layer.
  741. */
  742. static int sr_packet(struct cdrom_device_info *cdi,
  743. struct packet_command *cgc)
  744. {
  745. if (cgc->timeout <= 0)
  746. cgc->timeout = IOCTL_TIMEOUT;
  747. sr_do_ioctl(cdi->handle, cgc);
  748. return cgc->stat;
  749. }
  750. /**
  751. * sr_kref_release - Called to free the scsi_cd structure
  752. * @kref: pointer to embedded kref
  753. *
  754. * sr_ref_mutex must be held entering this routine. Because it is
  755. * called on last put, you should always use the scsi_cd_get()
  756. * scsi_cd_put() helpers which manipulate the semaphore directly
  757. * and never do a direct kref_put().
  758. **/
  759. static void sr_kref_release(struct kref *kref)
  760. {
  761. struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
  762. struct gendisk *disk = cd->disk;
  763. spin_lock(&sr_index_lock);
  764. clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
  765. spin_unlock(&sr_index_lock);
  766. unregister_cdrom(&cd->cdi);
  767. disk->private_data = NULL;
  768. put_disk(disk);
  769. kfree(cd);
  770. }
  771. static int sr_remove(struct device *dev)
  772. {
  773. struct scsi_cd *cd = dev_get_drvdata(dev);
  774. blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
  775. del_gendisk(cd->disk);
  776. mutex_lock(&sr_ref_mutex);
  777. kref_put(&cd->kref, sr_kref_release);
  778. mutex_unlock(&sr_ref_mutex);
  779. return 0;
  780. }
  781. static int __init init_sr(void)
  782. {
  783. int rc;
  784. rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
  785. if (rc)
  786. return rc;
  787. rc = scsi_register_driver(&sr_template.gendrv);
  788. if (rc)
  789. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  790. return rc;
  791. }
  792. static void __exit exit_sr(void)
  793. {
  794. scsi_unregister_driver(&sr_template.gendrv);
  795. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  796. }
  797. module_init(init_sr);
  798. module_exit(exit_sr);
  799. MODULE_LICENSE("GPL");