sr.c 23 KB

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