sr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. struct request *rq = SCpnt->request;
  285. if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
  286. return 0;
  287. memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
  288. SCpnt->cmd_len = rq->cmd_len;
  289. if (!rq->data_len)
  290. SCpnt->sc_data_direction = DMA_NONE;
  291. else if (rq_data_dir(rq) == WRITE)
  292. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  293. else
  294. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  295. this_count = rq->data_len;
  296. if (rq->timeout)
  297. timeout = rq->timeout;
  298. SCpnt->transfersize = rq->data_len;
  299. goto queue;
  300. }
  301. if (!(SCpnt->request->flags & REQ_CMD)) {
  302. blk_dump_rq_flags(SCpnt->request, "sr unsup command");
  303. return 0;
  304. }
  305. /*
  306. * we do lazy blocksize switching (when reading XA sectors,
  307. * see CDROMREADMODE2 ioctl)
  308. */
  309. s_size = cd->device->sector_size;
  310. if (s_size > 2048) {
  311. if (!in_interrupt())
  312. sr_set_blocklength(cd, 2048);
  313. else
  314. printk("sr: can't switch blocksize: in interrupt\n");
  315. }
  316. if (s_size != 512 && s_size != 1024 && s_size != 2048) {
  317. printk("sr: bad sector size %d\n", s_size);
  318. return 0;
  319. }
  320. if (rq_data_dir(SCpnt->request) == WRITE) {
  321. if (!cd->device->writeable)
  322. return 0;
  323. SCpnt->cmnd[0] = WRITE_10;
  324. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  325. cd->cdi.media_written = 1;
  326. } else if (rq_data_dir(SCpnt->request) == READ) {
  327. SCpnt->cmnd[0] = READ_10;
  328. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  329. } else {
  330. blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
  331. return 0;
  332. }
  333. {
  334. struct scatterlist *sg = SCpnt->request_buffer;
  335. int i, size = 0;
  336. for (i = 0; i < SCpnt->use_sg; i++)
  337. size += sg[i].length;
  338. if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
  339. printk(KERN_ERR "sr: mismatch count %d, bytes %d\n",
  340. size, SCpnt->request_bufflen);
  341. if (SCpnt->request_bufflen > size)
  342. SCpnt->request_bufflen = SCpnt->bufflen = size;
  343. }
  344. }
  345. /*
  346. * request doesn't start on hw block boundary, add scatter pads
  347. */
  348. if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
  349. (SCpnt->request_bufflen % s_size)) {
  350. printk("sr: unaligned transfer\n");
  351. return 0;
  352. }
  353. this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
  354. SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
  355. cd->cdi.name,
  356. (rq_data_dir(SCpnt->request) == WRITE) ?
  357. "writing" : "reading",
  358. this_count, SCpnt->request->nr_sectors));
  359. SCpnt->cmnd[1] = 0;
  360. block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
  361. if (this_count > 0xffff) {
  362. this_count = 0xffff;
  363. SCpnt->request_bufflen = SCpnt->bufflen =
  364. this_count * s_size;
  365. }
  366. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  367. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  368. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  369. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  370. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  371. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  372. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  373. /*
  374. * We shouldn't disconnect in the middle of a sector, so with a dumb
  375. * host adapter, it's safe to assume that we can at least transfer
  376. * this many bytes between each connect / disconnect.
  377. */
  378. SCpnt->transfersize = cd->device->sector_size;
  379. SCpnt->underflow = this_count << 9;
  380. queue:
  381. SCpnt->allowed = MAX_RETRIES;
  382. SCpnt->timeout_per_command = timeout;
  383. /*
  384. * This is the completion routine we use. This is matched in terms
  385. * of capability to this function.
  386. */
  387. SCpnt->done = rw_intr;
  388. /*
  389. * This indicates that the command is ready from our end to be
  390. * queued.
  391. */
  392. return 1;
  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 = scsi_cd(inode->i_bdev->bd_disk);
  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. /*
  421. * Send SCSI addressing ioctls directly to mid level, send other
  422. * ioctls to cdrom/block level.
  423. */
  424. switch (cmd) {
  425. case SCSI_IOCTL_GET_IDLUN:
  426. case SCSI_IOCTL_GET_BUS_NUMBER:
  427. return scsi_ioctl(sdev, cmd, (void __user *)arg);
  428. }
  429. return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
  430. }
  431. static int sr_block_media_changed(struct gendisk *disk)
  432. {
  433. struct scsi_cd *cd = scsi_cd(disk);
  434. return cdrom_media_changed(&cd->cdi);
  435. }
  436. static struct block_device_operations sr_bdops =
  437. {
  438. .owner = THIS_MODULE,
  439. .open = sr_block_open,
  440. .release = sr_block_release,
  441. .ioctl = sr_block_ioctl,
  442. .media_changed = sr_block_media_changed,
  443. /*
  444. * No compat_ioctl for now because sr_block_ioctl never
  445. * seems to pass arbitary ioctls down to host drivers.
  446. */
  447. };
  448. static int sr_open(struct cdrom_device_info *cdi, int purpose)
  449. {
  450. struct scsi_cd *cd = cdi->handle;
  451. struct scsi_device *sdev = cd->device;
  452. int retval;
  453. /*
  454. * If the device is in error recovery, wait until it is done.
  455. * If the device is offline, then disallow any access to it.
  456. */
  457. retval = -ENXIO;
  458. if (!scsi_block_when_processing_errors(sdev))
  459. goto error_out;
  460. return 0;
  461. error_out:
  462. return retval;
  463. }
  464. static void sr_release(struct cdrom_device_info *cdi)
  465. {
  466. struct scsi_cd *cd = cdi->handle;
  467. if (cd->device->sector_size > 2048)
  468. sr_set_blocklength(cd, 2048);
  469. }
  470. static int sr_probe(struct device *dev)
  471. {
  472. struct scsi_device *sdev = to_scsi_device(dev);
  473. struct gendisk *disk;
  474. struct scsi_cd *cd;
  475. int minor, error;
  476. error = -ENODEV;
  477. if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
  478. goto fail;
  479. error = -ENOMEM;
  480. cd = kmalloc(sizeof(*cd), GFP_KERNEL);
  481. if (!cd)
  482. goto fail;
  483. memset(cd, 0, sizeof(*cd));
  484. kref_init(&cd->kref);
  485. disk = alloc_disk(1);
  486. if (!disk)
  487. goto fail_free;
  488. spin_lock(&sr_index_lock);
  489. minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
  490. if (minor == SR_DISKS) {
  491. spin_unlock(&sr_index_lock);
  492. error = -EBUSY;
  493. goto fail_put;
  494. }
  495. __set_bit(minor, sr_index_bits);
  496. spin_unlock(&sr_index_lock);
  497. disk->major = SCSI_CDROM_MAJOR;
  498. disk->first_minor = minor;
  499. sprintf(disk->disk_name, "sr%d", minor);
  500. disk->fops = &sr_bdops;
  501. disk->flags = GENHD_FL_CD;
  502. cd->device = sdev;
  503. cd->disk = disk;
  504. cd->driver = &sr_template;
  505. cd->disk = disk;
  506. cd->capacity = 0x1fffff;
  507. cd->device->changed = 1; /* force recheck CD type */
  508. cd->use = 1;
  509. cd->readcd_known = 0;
  510. cd->readcd_cdda = 0;
  511. cd->cdi.ops = &sr_dops;
  512. cd->cdi.handle = cd;
  513. cd->cdi.mask = 0;
  514. cd->cdi.capacity = 1;
  515. sprintf(cd->cdi.name, "sr%d", minor);
  516. sdev->sector_size = 2048; /* A guess, just in case */
  517. /* FIXME: need to handle a get_capabilities failure properly ?? */
  518. get_capabilities(cd);
  519. sr_vendor_init(cd);
  520. snprintf(disk->devfs_name, sizeof(disk->devfs_name),
  521. "%s/cd", sdev->devfs_name);
  522. disk->driverfs_dev = &sdev->sdev_gendev;
  523. set_capacity(disk, cd->capacity);
  524. disk->private_data = &cd->driver;
  525. disk->queue = sdev->request_queue;
  526. cd->cdi.disk = disk;
  527. if (register_cdrom(&cd->cdi))
  528. goto fail_put;
  529. dev_set_drvdata(dev, cd);
  530. disk->flags |= GENHD_FL_REMOVABLE;
  531. add_disk(disk);
  532. printk(KERN_DEBUG
  533. "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n",
  534. cd->cdi.name, sdev->host->host_no, sdev->channel,
  535. sdev->id, sdev->lun);
  536. return 0;
  537. fail_put:
  538. put_disk(disk);
  539. fail_free:
  540. kfree(cd);
  541. fail:
  542. return error;
  543. }
  544. static void get_sectorsize(struct scsi_cd *cd)
  545. {
  546. unsigned char cmd[10];
  547. unsigned char *buffer;
  548. int the_result, retries = 3;
  549. int sector_size;
  550. request_queue_t *queue;
  551. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  552. if (!buffer)
  553. goto Enomem;
  554. do {
  555. cmd[0] = READ_CAPACITY;
  556. memset((void *) &cmd[1], 0, 9);
  557. memset(buffer, 0, 8);
  558. /* Do the command and wait.. */
  559. the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
  560. buffer, 8, NULL, SR_TIMEOUT,
  561. MAX_RETRIES);
  562. retries--;
  563. } while (the_result && retries);
  564. if (the_result) {
  565. cd->capacity = 0x1fffff;
  566. sector_size = 2048; /* A guess, just in case */
  567. } else {
  568. #if 0
  569. if (cdrom_get_last_written(&cd->cdi,
  570. &cd->capacity))
  571. #endif
  572. cd->capacity = 1 + ((buffer[0] << 24) |
  573. (buffer[1] << 16) |
  574. (buffer[2] << 8) |
  575. buffer[3]);
  576. sector_size = (buffer[4] << 24) |
  577. (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  578. switch (sector_size) {
  579. /*
  580. * HP 4020i CD-Recorder reports 2340 byte sectors
  581. * Philips CD-Writers report 2352 byte sectors
  582. *
  583. * Use 2k sectors for them..
  584. */
  585. case 0:
  586. case 2340:
  587. case 2352:
  588. sector_size = 2048;
  589. /* fall through */
  590. case 2048:
  591. cd->capacity *= 4;
  592. /* fall through */
  593. case 512:
  594. break;
  595. default:
  596. printk("%s: unsupported sector size %d.\n",
  597. cd->cdi.name, sector_size);
  598. cd->capacity = 0;
  599. }
  600. cd->device->sector_size = sector_size;
  601. /*
  602. * Add this so that we have the ability to correctly gauge
  603. * what the device is capable of.
  604. */
  605. set_capacity(cd->disk, cd->capacity);
  606. }
  607. queue = cd->device->request_queue;
  608. blk_queue_hardsect_size(queue, sector_size);
  609. out:
  610. kfree(buffer);
  611. return;
  612. Enomem:
  613. cd->capacity = 0x1fffff;
  614. cd->device->sector_size = 2048; /* A guess, just in case */
  615. goto out;
  616. }
  617. static void get_capabilities(struct scsi_cd *cd)
  618. {
  619. unsigned char *buffer;
  620. struct scsi_mode_data data;
  621. unsigned char cmd[MAX_COMMAND_SIZE];
  622. struct scsi_sense_hdr sshdr;
  623. unsigned int the_result;
  624. int retries, rc, n;
  625. static char *loadmech[] =
  626. {
  627. "caddy",
  628. "tray",
  629. "pop-up",
  630. "",
  631. "changer",
  632. "cartridge changer",
  633. "",
  634. ""
  635. };
  636. /* allocate transfer buffer */
  637. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  638. if (!buffer) {
  639. printk(KERN_ERR "sr: out of memory.\n");
  640. return;
  641. }
  642. /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
  643. * conditions are gone, or a timeout happens
  644. */
  645. retries = 0;
  646. do {
  647. memset((void *)cmd, 0, MAX_COMMAND_SIZE);
  648. cmd[0] = TEST_UNIT_READY;
  649. the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
  650. 0, &sshdr, SR_TIMEOUT,
  651. MAX_RETRIES);
  652. retries++;
  653. } while (retries < 5 &&
  654. (!scsi_status_is_good(the_result) ||
  655. (scsi_sense_valid(&sshdr) &&
  656. sshdr.sense_key == UNIT_ATTENTION)));
  657. /* ask for mode page 0x2a */
  658. rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
  659. SR_TIMEOUT, 3, &data, NULL);
  660. if (!scsi_status_is_good(rc)) {
  661. /* failed, drive doesn't have capabilities mode page */
  662. cd->cdi.speed = 1;
  663. cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
  664. CDC_DVD | CDC_DVD_RAM |
  665. CDC_SELECT_DISC | CDC_SELECT_SPEED);
  666. kfree(buffer);
  667. printk("%s: scsi-1 drive\n", cd->cdi.name);
  668. return;
  669. }
  670. n = data.header_length + data.block_descriptor_length;
  671. cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
  672. cd->readcd_known = 1;
  673. cd->readcd_cdda = buffer[n + 5] & 0x01;
  674. /* print some capability bits */
  675. printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
  676. ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
  677. cd->cdi.speed,
  678. buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
  679. buffer[n + 3] & 0x20 ? "dvd-ram " : "",
  680. buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
  681. buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
  682. buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
  683. loadmech[buffer[n + 6] >> 5]);
  684. if ((buffer[n + 6] >> 5) == 0)
  685. /* caddy drives can't close tray... */
  686. cd->cdi.mask |= CDC_CLOSE_TRAY;
  687. if ((buffer[n + 2] & 0x8) == 0)
  688. /* not a DVD drive */
  689. cd->cdi.mask |= CDC_DVD;
  690. if ((buffer[n + 3] & 0x20) == 0)
  691. /* can't write DVD-RAM media */
  692. cd->cdi.mask |= CDC_DVD_RAM;
  693. if ((buffer[n + 3] & 0x10) == 0)
  694. /* can't write DVD-R media */
  695. cd->cdi.mask |= CDC_DVD_R;
  696. if ((buffer[n + 3] & 0x2) == 0)
  697. /* can't write CD-RW media */
  698. cd->cdi.mask |= CDC_CD_RW;
  699. if ((buffer[n + 3] & 0x1) == 0)
  700. /* can't write CD-R media */
  701. cd->cdi.mask |= CDC_CD_R;
  702. if ((buffer[n + 6] & 0x8) == 0)
  703. /* can't eject */
  704. cd->cdi.mask |= CDC_OPEN_TRAY;
  705. if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
  706. (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
  707. cd->cdi.capacity =
  708. cdrom_number_of_slots(&cd->cdi);
  709. if (cd->cdi.capacity <= 1)
  710. /* not a changer */
  711. cd->cdi.mask |= CDC_SELECT_DISC;
  712. /*else I don't think it can close its tray
  713. cd->cdi.mask |= CDC_CLOSE_TRAY; */
  714. /*
  715. * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
  716. */
  717. if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
  718. (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
  719. cd->device->writeable = 1;
  720. }
  721. kfree(buffer);
  722. }
  723. /*
  724. * sr_packet() is the entry point for the generic commands generated
  725. * by the Uniform CD-ROM layer.
  726. */
  727. static int sr_packet(struct cdrom_device_info *cdi,
  728. struct packet_command *cgc)
  729. {
  730. if (cgc->timeout <= 0)
  731. cgc->timeout = IOCTL_TIMEOUT;
  732. sr_do_ioctl(cdi->handle, cgc);
  733. return cgc->stat;
  734. }
  735. /**
  736. * sr_kref_release - Called to free the scsi_cd structure
  737. * @kref: pointer to embedded kref
  738. *
  739. * sr_ref_sem must be held entering this routine. Because it is
  740. * called on last put, you should always use the scsi_cd_get()
  741. * scsi_cd_put() helpers which manipulate the semaphore directly
  742. * and never do a direct kref_put().
  743. **/
  744. static void sr_kref_release(struct kref *kref)
  745. {
  746. struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
  747. struct gendisk *disk = cd->disk;
  748. spin_lock(&sr_index_lock);
  749. clear_bit(disk->first_minor, sr_index_bits);
  750. spin_unlock(&sr_index_lock);
  751. unregister_cdrom(&cd->cdi);
  752. disk->private_data = NULL;
  753. put_disk(disk);
  754. kfree(cd);
  755. }
  756. static int sr_remove(struct device *dev)
  757. {
  758. struct scsi_cd *cd = dev_get_drvdata(dev);
  759. del_gendisk(cd->disk);
  760. down(&sr_ref_sem);
  761. kref_put(&cd->kref, sr_kref_release);
  762. up(&sr_ref_sem);
  763. return 0;
  764. }
  765. static int __init init_sr(void)
  766. {
  767. int rc;
  768. rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
  769. if (rc)
  770. return rc;
  771. return scsi_register_driver(&sr_template.gendrv);
  772. }
  773. static void __exit exit_sr(void)
  774. {
  775. scsi_unregister_driver(&sr_template.gendrv);
  776. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  777. }
  778. module_init(init_sr);
  779. module_exit(exit_sr);
  780. MODULE_LICENSE("GPL");