sr.c 23 KB

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