sr.c 23 KB

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