sr.c 23 KB

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