sr.c 23 KB

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