sr.c 23 KB

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