sr.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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_eh.h>
  52. #include <scsi/scsi_host.h>
  53. #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
  54. #include <scsi/scsi_request.h>
  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. /*
  176. * If the disk changed, the capacity will now be different,
  177. * so we force a re-read of this information
  178. * Force 2048 for the sector size so that filesystems won't
  179. * be trying to use something that is too small if the disc
  180. * has changed.
  181. */
  182. cd->needs_sector_size = 1;
  183. cd->device->sector_size = 2048;
  184. }
  185. return retval;
  186. }
  187. /*
  188. * rw_intr is the interrupt routine for the device driver.
  189. *
  190. * It will be notified on the end of a SCSI read / write, and will take on
  191. * of several actions based on success or failure.
  192. */
  193. static void rw_intr(struct scsi_cmnd * SCpnt)
  194. {
  195. int result = SCpnt->result;
  196. int this_count = SCpnt->bufflen;
  197. int good_bytes = (result == 0 ? this_count : 0);
  198. int block_sectors = 0;
  199. long error_sector;
  200. struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
  201. #ifdef DEBUG
  202. printk("sr.c done: %x\n", result);
  203. #endif
  204. /*
  205. * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
  206. * success. Since this is a relatively rare error condition, no
  207. * care is taken to avoid unnecessary additional work such as
  208. * memcpy's that could be avoided.
  209. */
  210. if (driver_byte(result) != 0 && /* An error occurred */
  211. (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
  212. switch (SCpnt->sense_buffer[2]) {
  213. case MEDIUM_ERROR:
  214. case VOLUME_OVERFLOW:
  215. case ILLEGAL_REQUEST:
  216. if (!(SCpnt->sense_buffer[0] & 0x90))
  217. break;
  218. if (!blk_fs_request(SCpnt->request))
  219. break;
  220. error_sector = (SCpnt->sense_buffer[3] << 24) |
  221. (SCpnt->sense_buffer[4] << 16) |
  222. (SCpnt->sense_buffer[5] << 8) |
  223. SCpnt->sense_buffer[6];
  224. if (SCpnt->request->bio != NULL)
  225. block_sectors =
  226. bio_sectors(SCpnt->request->bio);
  227. if (block_sectors < 4)
  228. block_sectors = 4;
  229. if (cd->device->sector_size == 2048)
  230. error_sector <<= 2;
  231. error_sector &= ~(block_sectors - 1);
  232. good_bytes = (error_sector - SCpnt->request->sector) << 9;
  233. if (good_bytes < 0 || good_bytes >= this_count)
  234. good_bytes = 0;
  235. /*
  236. * The SCSI specification allows for the value
  237. * returned by READ CAPACITY to be up to 75 2K
  238. * sectors past the last readable block.
  239. * Therefore, if we hit a medium error within the
  240. * last 75 2K sectors, we decrease the saved size
  241. * value.
  242. */
  243. if (error_sector < get_capacity(cd->disk) &&
  244. cd->capacity - error_sector < 4 * 75)
  245. set_capacity(cd->disk, error_sector);
  246. break;
  247. case RECOVERED_ERROR:
  248. /*
  249. * An error occured, but it recovered. Inform the
  250. * user, but make sure that it's not treated as a
  251. * hard error.
  252. */
  253. scsi_print_sense("sr", SCpnt);
  254. SCpnt->result = 0;
  255. SCpnt->sense_buffer[0] = 0x0;
  256. good_bytes = this_count;
  257. break;
  258. default:
  259. break;
  260. }
  261. }
  262. /*
  263. * This calls the generic completion function, now that we know
  264. * how many actual sectors finished, and how many sectors we need
  265. * to say have failed.
  266. */
  267. scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
  268. }
  269. static int sr_init_command(struct scsi_cmnd * SCpnt)
  270. {
  271. int block=0, this_count, s_size, timeout = SR_TIMEOUT;
  272. struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
  273. SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
  274. cd->disk->disk_name, block));
  275. if (!cd->device || !scsi_device_online(cd->device)) {
  276. SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
  277. SCpnt->request->nr_sectors));
  278. SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
  279. return 0;
  280. }
  281. if (cd->device->changed) {
  282. /*
  283. * quietly refuse to do anything to a changed disc until the
  284. * changed bit has been reset
  285. */
  286. return 0;
  287. }
  288. /*
  289. * these are already setup, just copy cdb basically
  290. */
  291. if (SCpnt->request->flags & REQ_BLOCK_PC) {
  292. struct request *rq = SCpnt->request;
  293. if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
  294. return 0;
  295. memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
  296. if (!rq->data_len)
  297. SCpnt->sc_data_direction = DMA_NONE;
  298. else if (rq_data_dir(rq) == WRITE)
  299. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  300. else
  301. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  302. this_count = rq->data_len;
  303. if (rq->timeout)
  304. timeout = rq->timeout;
  305. SCpnt->transfersize = rq->data_len;
  306. goto queue;
  307. }
  308. if (!(SCpnt->request->flags & REQ_CMD)) {
  309. blk_dump_rq_flags(SCpnt->request, "sr unsup command");
  310. return 0;
  311. }
  312. /*
  313. * we do lazy blocksize switching (when reading XA sectors,
  314. * see CDROMREADMODE2 ioctl)
  315. */
  316. s_size = cd->device->sector_size;
  317. if (s_size > 2048) {
  318. if (!in_interrupt())
  319. sr_set_blocklength(cd, 2048);
  320. else
  321. printk("sr: can't switch blocksize: in interrupt\n");
  322. }
  323. if (s_size != 512 && s_size != 1024 && s_size != 2048) {
  324. printk("sr: bad sector size %d\n", s_size);
  325. return 0;
  326. }
  327. if (rq_data_dir(SCpnt->request) == WRITE) {
  328. if (!cd->device->writeable)
  329. return 0;
  330. SCpnt->cmnd[0] = WRITE_10;
  331. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  332. cd->cdi.media_written = 1;
  333. } else if (rq_data_dir(SCpnt->request) == READ) {
  334. SCpnt->cmnd[0] = READ_10;
  335. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  336. } else {
  337. blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
  338. return 0;
  339. }
  340. {
  341. struct scatterlist *sg = SCpnt->request_buffer;
  342. int i, size = 0;
  343. for (i = 0; i < SCpnt->use_sg; i++)
  344. size += sg[i].length;
  345. if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
  346. printk(KERN_ERR "sr: mismatch count %d, bytes %d\n",
  347. size, SCpnt->request_bufflen);
  348. if (SCpnt->request_bufflen > size)
  349. SCpnt->request_bufflen = SCpnt->bufflen = size;
  350. }
  351. }
  352. /*
  353. * request doesn't start on hw block boundary, add scatter pads
  354. */
  355. if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
  356. (SCpnt->request_bufflen % s_size)) {
  357. printk("sr: unaligned transfer\n");
  358. return 0;
  359. }
  360. this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
  361. SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
  362. cd->cdi.name,
  363. (rq_data_dir(SCpnt->request) == WRITE) ?
  364. "writing" : "reading",
  365. this_count, SCpnt->request->nr_sectors));
  366. SCpnt->cmnd[1] = 0;
  367. block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
  368. if (this_count > 0xffff) {
  369. this_count = 0xffff;
  370. SCpnt->request_bufflen = SCpnt->bufflen =
  371. this_count * s_size;
  372. }
  373. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  374. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  375. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  376. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  377. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  378. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  379. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  380. /*
  381. * We shouldn't disconnect in the middle of a sector, so with a dumb
  382. * host adapter, it's safe to assume that we can at least transfer
  383. * this many bytes between each connect / disconnect.
  384. */
  385. SCpnt->transfersize = cd->device->sector_size;
  386. SCpnt->underflow = this_count << 9;
  387. queue:
  388. SCpnt->allowed = MAX_RETRIES;
  389. SCpnt->timeout_per_command = timeout;
  390. /*
  391. * This is the completion routine we use. This is matched in terms
  392. * of capability to this function.
  393. */
  394. SCpnt->done = rw_intr;
  395. /*
  396. * This indicates that the command is ready from our end to be
  397. * queued.
  398. */
  399. return 1;
  400. }
  401. static int sr_block_open(struct inode *inode, struct file *file)
  402. {
  403. struct gendisk *disk = inode->i_bdev->bd_disk;
  404. struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
  405. int ret = 0;
  406. if(!(cd = scsi_cd_get(disk)))
  407. return -ENXIO;
  408. if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
  409. scsi_cd_put(cd);
  410. return ret;
  411. }
  412. static int sr_block_release(struct inode *inode, struct file *file)
  413. {
  414. int ret;
  415. struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
  416. ret = cdrom_release(&cd->cdi, file);
  417. if(ret)
  418. return ret;
  419. scsi_cd_put(cd);
  420. return 0;
  421. }
  422. static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
  423. unsigned long arg)
  424. {
  425. struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
  426. struct scsi_device *sdev = cd->device;
  427. /*
  428. * Send SCSI addressing ioctls directly to mid level, send other
  429. * ioctls to cdrom/block level.
  430. */
  431. switch (cmd) {
  432. case SCSI_IOCTL_GET_IDLUN:
  433. case SCSI_IOCTL_GET_BUS_NUMBER:
  434. return scsi_ioctl(sdev, cmd, (void __user *)arg);
  435. }
  436. return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
  437. }
  438. static int sr_block_media_changed(struct gendisk *disk)
  439. {
  440. struct scsi_cd *cd = scsi_cd(disk);
  441. return cdrom_media_changed(&cd->cdi);
  442. }
  443. static struct block_device_operations sr_bdops =
  444. {
  445. .owner = THIS_MODULE,
  446. .open = sr_block_open,
  447. .release = sr_block_release,
  448. .ioctl = sr_block_ioctl,
  449. .media_changed = sr_block_media_changed,
  450. /*
  451. * No compat_ioctl for now because sr_block_ioctl never
  452. * seems to pass arbitary ioctls down to host drivers.
  453. */
  454. };
  455. static int sr_open(struct cdrom_device_info *cdi, int purpose)
  456. {
  457. struct scsi_cd *cd = cdi->handle;
  458. struct scsi_device *sdev = cd->device;
  459. int retval;
  460. /*
  461. * If the device is in error recovery, wait until it is done.
  462. * If the device is offline, then disallow any access to it.
  463. */
  464. retval = -ENXIO;
  465. if (!scsi_block_when_processing_errors(sdev))
  466. goto error_out;
  467. /*
  468. * If this device did not have media in the drive at boot time, then
  469. * we would have been unable to get the sector size. Check to see if
  470. * this is the case, and try again.
  471. */
  472. if (cd->needs_sector_size)
  473. get_sectorsize(cd);
  474. return 0;
  475. error_out:
  476. return retval;
  477. }
  478. static void sr_release(struct cdrom_device_info *cdi)
  479. {
  480. struct scsi_cd *cd = cdi->handle;
  481. if (cd->device->sector_size > 2048)
  482. sr_set_blocklength(cd, 2048);
  483. }
  484. static int sr_probe(struct device *dev)
  485. {
  486. struct scsi_device *sdev = to_scsi_device(dev);
  487. struct gendisk *disk;
  488. struct scsi_cd *cd;
  489. int minor, error;
  490. error = -ENODEV;
  491. if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
  492. goto fail;
  493. error = -ENOMEM;
  494. cd = kmalloc(sizeof(*cd), GFP_KERNEL);
  495. if (!cd)
  496. goto fail;
  497. memset(cd, 0, sizeof(*cd));
  498. kref_init(&cd->kref);
  499. disk = alloc_disk(1);
  500. if (!disk)
  501. goto fail_free;
  502. spin_lock(&sr_index_lock);
  503. minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
  504. if (minor == SR_DISKS) {
  505. spin_unlock(&sr_index_lock);
  506. error = -EBUSY;
  507. goto fail_put;
  508. }
  509. __set_bit(minor, sr_index_bits);
  510. spin_unlock(&sr_index_lock);
  511. disk->major = SCSI_CDROM_MAJOR;
  512. disk->first_minor = minor;
  513. sprintf(disk->disk_name, "sr%d", minor);
  514. disk->fops = &sr_bdops;
  515. disk->flags = GENHD_FL_CD;
  516. cd->device = sdev;
  517. cd->disk = disk;
  518. cd->driver = &sr_template;
  519. cd->disk = disk;
  520. cd->capacity = 0x1fffff;
  521. cd->needs_sector_size = 1;
  522. cd->device->changed = 1; /* force recheck CD type */
  523. cd->use = 1;
  524. cd->readcd_known = 0;
  525. cd->readcd_cdda = 0;
  526. cd->cdi.ops = &sr_dops;
  527. cd->cdi.handle = cd;
  528. cd->cdi.mask = 0;
  529. cd->cdi.capacity = 1;
  530. sprintf(cd->cdi.name, "sr%d", minor);
  531. sdev->sector_size = 2048; /* A guess, just in case */
  532. /* FIXME: need to handle a get_capabilities failure properly ?? */
  533. get_capabilities(cd);
  534. sr_vendor_init(cd);
  535. snprintf(disk->devfs_name, sizeof(disk->devfs_name),
  536. "%s/cd", sdev->devfs_name);
  537. disk->driverfs_dev = &sdev->sdev_gendev;
  538. set_capacity(disk, cd->capacity);
  539. disk->private_data = &cd->driver;
  540. disk->queue = sdev->request_queue;
  541. cd->cdi.disk = disk;
  542. if (register_cdrom(&cd->cdi))
  543. goto fail_put;
  544. dev_set_drvdata(dev, cd);
  545. disk->flags |= GENHD_FL_REMOVABLE;
  546. add_disk(disk);
  547. printk(KERN_DEBUG
  548. "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n",
  549. cd->cdi.name, sdev->host->host_no, sdev->channel,
  550. sdev->id, sdev->lun);
  551. return 0;
  552. fail_put:
  553. put_disk(disk);
  554. fail_free:
  555. kfree(cd);
  556. fail:
  557. return error;
  558. }
  559. static void get_sectorsize(struct scsi_cd *cd)
  560. {
  561. unsigned char cmd[10];
  562. unsigned char *buffer;
  563. int the_result, retries = 3;
  564. int sector_size;
  565. struct scsi_request *SRpnt = NULL;
  566. request_queue_t *queue;
  567. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  568. if (!buffer)
  569. goto Enomem;
  570. SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
  571. if (!SRpnt)
  572. goto Enomem;
  573. do {
  574. cmd[0] = READ_CAPACITY;
  575. memset((void *) &cmd[1], 0, 9);
  576. /* Mark as really busy */
  577. SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
  578. SRpnt->sr_cmd_len = 0;
  579. memset(buffer, 0, 8);
  580. /* Do the command and wait.. */
  581. SRpnt->sr_data_direction = DMA_FROM_DEVICE;
  582. scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
  583. 8, SR_TIMEOUT, MAX_RETRIES);
  584. the_result = SRpnt->sr_result;
  585. retries--;
  586. } while (the_result && retries);
  587. scsi_release_request(SRpnt);
  588. SRpnt = NULL;
  589. if (the_result) {
  590. cd->capacity = 0x1fffff;
  591. sector_size = 2048; /* A guess, just in case */
  592. cd->needs_sector_size = 1;
  593. } else {
  594. #if 0
  595. if (cdrom_get_last_written(&cd->cdi,
  596. &cd->capacity))
  597. #endif
  598. cd->capacity = 1 + ((buffer[0] << 24) |
  599. (buffer[1] << 16) |
  600. (buffer[2] << 8) |
  601. buffer[3]);
  602. sector_size = (buffer[4] << 24) |
  603. (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  604. switch (sector_size) {
  605. /*
  606. * HP 4020i CD-Recorder reports 2340 byte sectors
  607. * Philips CD-Writers report 2352 byte sectors
  608. *
  609. * Use 2k sectors for them..
  610. */
  611. case 0:
  612. case 2340:
  613. case 2352:
  614. sector_size = 2048;
  615. /* fall through */
  616. case 2048:
  617. cd->capacity *= 4;
  618. /* fall through */
  619. case 512:
  620. break;
  621. default:
  622. printk("%s: unsupported sector size %d.\n",
  623. cd->cdi.name, sector_size);
  624. cd->capacity = 0;
  625. cd->needs_sector_size = 1;
  626. }
  627. cd->device->sector_size = sector_size;
  628. /*
  629. * Add this so that we have the ability to correctly gauge
  630. * what the device is capable of.
  631. */
  632. cd->needs_sector_size = 0;
  633. set_capacity(cd->disk, cd->capacity);
  634. }
  635. queue = cd->device->request_queue;
  636. blk_queue_hardsect_size(queue, sector_size);
  637. out:
  638. kfree(buffer);
  639. return;
  640. Enomem:
  641. cd->capacity = 0x1fffff;
  642. sector_size = 2048; /* A guess, just in case */
  643. cd->needs_sector_size = 1;
  644. if (SRpnt)
  645. scsi_release_request(SRpnt);
  646. goto out;
  647. }
  648. static void get_capabilities(struct scsi_cd *cd)
  649. {
  650. unsigned char *buffer;
  651. struct scsi_mode_data data;
  652. struct scsi_request *SRpnt;
  653. unsigned char cmd[MAX_COMMAND_SIZE];
  654. unsigned int the_result;
  655. int retries, rc, n;
  656. static char *loadmech[] =
  657. {
  658. "caddy",
  659. "tray",
  660. "pop-up",
  661. "",
  662. "changer",
  663. "cartridge changer",
  664. "",
  665. ""
  666. };
  667. /* allocate a request for the TEST_UNIT_READY */
  668. SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
  669. if (!SRpnt) {
  670. printk(KERN_WARNING "(get_capabilities:) Request allocation "
  671. "failure.\n");
  672. return;
  673. }
  674. /* allocate transfer buffer */
  675. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  676. if (!buffer) {
  677. printk(KERN_ERR "sr: out of memory.\n");
  678. scsi_release_request(SRpnt);
  679. return;
  680. }
  681. /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
  682. * conditions are gone, or a timeout happens
  683. */
  684. retries = 0;
  685. do {
  686. memset((void *)cmd, 0, MAX_COMMAND_SIZE);
  687. cmd[0] = TEST_UNIT_READY;
  688. SRpnt->sr_cmd_len = 0;
  689. SRpnt->sr_sense_buffer[0] = 0;
  690. SRpnt->sr_sense_buffer[2] = 0;
  691. SRpnt->sr_data_direction = DMA_NONE;
  692. scsi_wait_req (SRpnt, (void *) cmd, buffer,
  693. 0, SR_TIMEOUT, MAX_RETRIES);
  694. the_result = SRpnt->sr_result;
  695. retries++;
  696. } while (retries < 5 &&
  697. (!scsi_status_is_good(the_result) ||
  698. ((driver_byte(the_result) & DRIVER_SENSE) &&
  699. SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION)));
  700. /* ask for mode page 0x2a */
  701. rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
  702. SR_TIMEOUT, 3, &data);
  703. if (!scsi_status_is_good(rc)) {
  704. /* failed, drive doesn't have capabilities mode page */
  705. cd->cdi.speed = 1;
  706. cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
  707. CDC_DVD | CDC_DVD_RAM |
  708. CDC_SELECT_DISC | CDC_SELECT_SPEED);
  709. scsi_release_request(SRpnt);
  710. kfree(buffer);
  711. printk("%s: scsi-1 drive\n", cd->cdi.name);
  712. return;
  713. }
  714. n = data.header_length + data.block_descriptor_length;
  715. cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
  716. cd->readcd_known = 1;
  717. cd->readcd_cdda = buffer[n + 5] & 0x01;
  718. /* print some capability bits */
  719. printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
  720. ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
  721. cd->cdi.speed,
  722. buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
  723. buffer[n + 3] & 0x20 ? "dvd-ram " : "",
  724. buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
  725. buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
  726. buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
  727. loadmech[buffer[n + 6] >> 5]);
  728. if ((buffer[n + 6] >> 5) == 0)
  729. /* caddy drives can't close tray... */
  730. cd->cdi.mask |= CDC_CLOSE_TRAY;
  731. if ((buffer[n + 2] & 0x8) == 0)
  732. /* not a DVD drive */
  733. cd->cdi.mask |= CDC_DVD;
  734. if ((buffer[n + 3] & 0x20) == 0)
  735. /* can't write DVD-RAM media */
  736. cd->cdi.mask |= CDC_DVD_RAM;
  737. if ((buffer[n + 3] & 0x10) == 0)
  738. /* can't write DVD-R media */
  739. cd->cdi.mask |= CDC_DVD_R;
  740. if ((buffer[n + 3] & 0x2) == 0)
  741. /* can't write CD-RW media */
  742. cd->cdi.mask |= CDC_CD_RW;
  743. if ((buffer[n + 3] & 0x1) == 0)
  744. /* can't write CD-R media */
  745. cd->cdi.mask |= CDC_CD_R;
  746. if ((buffer[n + 6] & 0x8) == 0)
  747. /* can't eject */
  748. cd->cdi.mask |= CDC_OPEN_TRAY;
  749. if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
  750. (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
  751. cd->cdi.capacity =
  752. cdrom_number_of_slots(&cd->cdi);
  753. if (cd->cdi.capacity <= 1)
  754. /* not a changer */
  755. cd->cdi.mask |= CDC_SELECT_DISC;
  756. /*else I don't think it can close its tray
  757. cd->cdi.mask |= CDC_CLOSE_TRAY; */
  758. /*
  759. * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
  760. */
  761. if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
  762. (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
  763. cd->device->writeable = 1;
  764. }
  765. scsi_release_request(SRpnt);
  766. kfree(buffer);
  767. }
  768. /*
  769. * sr_packet() is the entry point for the generic commands generated
  770. * by the Uniform CD-ROM layer.
  771. */
  772. static int sr_packet(struct cdrom_device_info *cdi,
  773. struct packet_command *cgc)
  774. {
  775. if (cgc->timeout <= 0)
  776. cgc->timeout = IOCTL_TIMEOUT;
  777. sr_do_ioctl(cdi->handle, cgc);
  778. return cgc->stat;
  779. }
  780. /**
  781. * sr_kref_release - Called to free the scsi_cd structure
  782. * @kref: pointer to embedded kref
  783. *
  784. * sr_ref_sem must be held entering this routine. Because it is
  785. * called on last put, you should always use the scsi_cd_get()
  786. * scsi_cd_put() helpers which manipulate the semaphore directly
  787. * and never do a direct kref_put().
  788. **/
  789. static void sr_kref_release(struct kref *kref)
  790. {
  791. struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
  792. struct gendisk *disk = cd->disk;
  793. spin_lock(&sr_index_lock);
  794. clear_bit(disk->first_minor, sr_index_bits);
  795. spin_unlock(&sr_index_lock);
  796. unregister_cdrom(&cd->cdi);
  797. disk->private_data = NULL;
  798. put_disk(disk);
  799. kfree(cd);
  800. }
  801. static int sr_remove(struct device *dev)
  802. {
  803. struct scsi_cd *cd = dev_get_drvdata(dev);
  804. del_gendisk(cd->disk);
  805. down(&sr_ref_sem);
  806. kref_put(&cd->kref, sr_kref_release);
  807. up(&sr_ref_sem);
  808. return 0;
  809. }
  810. static int __init init_sr(void)
  811. {
  812. int rc;
  813. rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
  814. if (rc)
  815. return rc;
  816. return scsi_register_driver(&sr_template.gendrv);
  817. }
  818. static void __exit exit_sr(void)
  819. {
  820. scsi_unregister_driver(&sr_template.gendrv);
  821. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  822. }
  823. module_init(init_sr);
  824. module_exit(exit_sr);
  825. MODULE_LICENSE("GPL");