sr.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:26k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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 loose 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.  */
  35. #include <linux/module.h>
  36. #include <linux/fs.h>
  37. #include <linux/kernel.h>
  38. #include <linux/sched.h>
  39. #include <linux/mm.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 <asm/system.h>
  46. #include <asm/io.h>
  47. #include <asm/uaccess.h>
  48. #define MAJOR_NR SCSI_CDROM_MAJOR
  49. #include <linux/blk.h>
  50. #include "scsi.h"
  51. #include "hosts.h"
  52. #include "sr.h"
  53. #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
  54. #include "constants.h"
  55. MODULE_PARM(xa_test, "i"); /* see sr_ioctl.c */
  56. #define MAX_RETRIES 3
  57. #define SR_TIMEOUT (30 * HZ)
  58. static int sr_init(void);
  59. static void sr_finish(void);
  60. static int sr_attach(Scsi_Device *);
  61. static int sr_detect(Scsi_Device *);
  62. static void sr_detach(Scsi_Device *);
  63. static int sr_init_command(Scsi_Cmnd *);
  64. static struct Scsi_Device_Template sr_template =
  65. {
  66. name:"cdrom",
  67. tag:"sr",
  68. scsi_type:TYPE_ROM,
  69. major:SCSI_CDROM_MAJOR,
  70. blk:1,
  71. detect:sr_detect,
  72. init:sr_init,
  73. finish:sr_finish,
  74. attach:sr_attach,
  75. detach:sr_detach,
  76. init_command:sr_init_command
  77. };
  78. Scsi_CD *scsi_CDs;
  79. static int *sr_sizes;
  80. static int *sr_blocksizes;
  81. static int *sr_hardsizes;
  82. static int sr_open(struct cdrom_device_info *, int);
  83. void get_sectorsize(int);
  84. void get_capabilities(int);
  85. static int sr_media_change(struct cdrom_device_info *, int);
  86. static int sr_packet(struct cdrom_device_info *, struct cdrom_generic_command *);
  87. static void sr_release(struct cdrom_device_info *cdi)
  88. {
  89. if (scsi_CDs[MINOR(cdi->dev)].device->sector_size > 2048)
  90. sr_set_blocklength(MINOR(cdi->dev), 2048);
  91. scsi_CDs[MINOR(cdi->dev)].device->access_count--;
  92. if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
  93. __MOD_DEC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
  94. if (sr_template.module)
  95. __MOD_DEC_USE_COUNT(sr_template.module);
  96. }
  97. static struct cdrom_device_ops sr_dops =
  98. {
  99. open: sr_open,
  100. release: sr_release,
  101. drive_status: sr_drive_status,
  102. media_changed: sr_media_change,
  103. tray_move: sr_tray_move,
  104. lock_door: sr_lock_door,
  105. select_speed: sr_select_speed,
  106. get_last_session: sr_get_last_session,
  107. get_mcn: sr_get_mcn,
  108. reset: sr_reset,
  109. audio_ioctl: sr_audio_ioctl,
  110. dev_ioctl: sr_dev_ioctl,
  111. capability: CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
  112. CDC_SELECT_SPEED | CDC_SELECT_DISC |
  113. CDC_MULTI_SESSION | CDC_MCN |
  114. CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
  115. CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS |
  116. CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
  117. CDC_DVD_RAM | CDC_GENERIC_PACKET,
  118. generic_packet: sr_packet,
  119. };
  120. /*
  121.  * This function checks to see if the media has been changed in the
  122.  * CDROM drive.  It is possible that we have already sensed a change,
  123.  * or the drive may have sensed one and not yet reported it.  We must
  124.  * be ready for either case. This function always reports the current
  125.  * value of the changed bit.  If flag is 0, then the changed bit is reset.
  126.  * This function could be done as an ioctl, but we would need to have
  127.  * an inode for that to work, and we do not always have one.
  128.  */
  129. int sr_media_change(struct cdrom_device_info *cdi, int slot)
  130. {
  131. int retval;
  132. if (CDSL_CURRENT != slot) {
  133. /* no changer support */
  134. return -EINVAL;
  135. }
  136. retval = scsi_ioctl(scsi_CDs[MINOR(cdi->dev)].device,
  137.     SCSI_IOCTL_TEST_UNIT_READY, 0);
  138. if (retval) {
  139. /* Unable to test, unit probably not ready.  This usually
  140.  * means there is no disc in the drive.  Mark as changed,
  141.  * and we will figure it out later once the drive is
  142.  * available again.  */
  143. scsi_CDs[MINOR(cdi->dev)].device->changed = 1;
  144. return 1; /* This will force a flush, if called from
  145.  * check_disk_change */
  146. };
  147. retval = scsi_CDs[MINOR(cdi->dev)].device->changed;
  148. scsi_CDs[MINOR(cdi->dev)].device->changed = 0;
  149. /* If the disk changed, the capacity will now be different,
  150.  * so we force a re-read of this information */
  151. if (retval) {
  152. /* check multisession offset etc */
  153. sr_cd_check(cdi);
  154. /* 
  155.  * If the disk changed, the capacity will now be different,
  156.  * so we force a re-read of this information 
  157.  * Force 2048 for the sector size so that filesystems won't
  158.  * be trying to use something that is too small if the disc
  159.  * has changed.
  160.  */
  161. scsi_CDs[MINOR(cdi->dev)].needs_sector_size = 1;
  162. scsi_CDs[MINOR(cdi->dev)].device->sector_size = 2048;
  163. }
  164. return retval;
  165. }
  166. /*
  167.  * rw_intr is the interrupt routine for the device driver.  It will be notified on the
  168.  * end of a SCSI read / write, and will take on of several actions based on success or failure.
  169.  */
  170. static void rw_intr(Scsi_Cmnd * SCpnt)
  171. {
  172. int result = SCpnt->result;
  173. int this_count = SCpnt->bufflen >> 9;
  174. int good_sectors = (result == 0 ? this_count : 0);
  175. int block_sectors = 0;
  176. int device_nr = DEVICE_NR(SCpnt->request.rq_dev);
  177. long error_sector;
  178. #ifdef DEBUG
  179. printk("sr.c done: %x %pn", result, SCpnt->request.bh->b_data);
  180. #endif
  181. /*
  182.    Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial success.
  183.    Since this is a relatively rare error condition, no care is taken to
  184.    avoid unnecessary additional work such as memcpy's that could be avoided.
  185.  */
  186. if (driver_byte(result) != 0 && /* An error occurred */
  187.     SCpnt->sense_buffer[0] == 0xF0) { /* Sense data is valid */
  188. switch (SCpnt->sense_buffer[2]) {
  189. case MEDIUM_ERROR:
  190. case VOLUME_OVERFLOW:
  191. case ILLEGAL_REQUEST:
  192. error_sector = (SCpnt->sense_buffer[3] << 24) |
  193. (SCpnt->sense_buffer[4] << 16) |
  194. (SCpnt->sense_buffer[5] << 8) |
  195. SCpnt->sense_buffer[6];
  196. if (SCpnt->request.bh != NULL)
  197. block_sectors = SCpnt->request.bh->b_size >> 9;
  198. if (block_sectors < 4)
  199. block_sectors = 4;
  200. if (scsi_CDs[device_nr].device->sector_size == 2048)
  201. error_sector <<= 2;
  202. error_sector &= ~(block_sectors - 1);
  203. good_sectors = error_sector - SCpnt->request.sector;
  204. if (good_sectors < 0 || good_sectors >= this_count)
  205. good_sectors = 0;
  206. /*
  207.  * The SCSI specification allows for the value returned
  208.  * by READ CAPACITY to be up to 75 2K sectors past the
  209.  * last readable block.  Therefore, if we hit a medium
  210.  * error within the last 75 2K sectors, we decrease the
  211.  * saved size value.
  212.  */
  213. if ((error_sector >> 1) < sr_sizes[device_nr] &&
  214.     scsi_CDs[device_nr].capacity - error_sector < 4 *75)
  215. sr_sizes[device_nr] = error_sector >> 1;
  216. break;
  217. case RECOVERED_ERROR:
  218. /*
  219.  * An error occured, but it recovered.  Inform the
  220.  * user, but make sure that it's not treated as a
  221.  * hard error.
  222.  */
  223. print_sense("sr", SCpnt);
  224. result = 0;
  225. SCpnt->sense_buffer[0] = 0x0;
  226. good_sectors = this_count;
  227. break;
  228. default:
  229. break;
  230. }
  231. }
  232. /*
  233.  * This calls the generic completion function, now that we know
  234.  * how many actual sectors finished, and how many sectors we need
  235.  * to say have failed.
  236.  */
  237. scsi_io_completion(SCpnt, good_sectors, block_sectors);
  238. }
  239. static request_queue_t *sr_find_queue(kdev_t dev)
  240. {
  241. /*
  242.  * No such device
  243.  */
  244. if (MINOR(dev) >= sr_template.dev_max || !scsi_CDs[MINOR(dev)].device)
  245. return NULL;
  246. return &scsi_CDs[MINOR(dev)].device->request_queue;
  247. }
  248. static int sr_scatter_pad(Scsi_Cmnd *SCpnt, int s_size)
  249. {
  250. struct scatterlist *sg, *old_sg = NULL;
  251. int i, fsize, bsize, sg_ent, sg_count;
  252. char *front, *back;
  253. void **bbpnt, **old_bbpnt = NULL;
  254. back = front = NULL;
  255. sg_ent = SCpnt->use_sg;
  256. bsize = 0; /* gcc... */
  257. /*
  258.  * need front pad
  259.  */
  260. if ((fsize = SCpnt->request.sector % (s_size >> 9))) {
  261. fsize <<= 9;
  262. sg_ent++;
  263. if ((front = scsi_malloc(fsize)) == NULL)
  264. goto no_mem;
  265. }
  266. /*
  267.  * need a back pad too
  268.  */
  269. if ((bsize = s_size - ((SCpnt->request_bufflen + fsize) % s_size))) {
  270. sg_ent++;
  271. if ((back = scsi_malloc(bsize)) == NULL)
  272. goto no_mem;
  273. }
  274. /*
  275.  * extend or allocate new scatter-gather table
  276.  */
  277. sg_count = SCpnt->use_sg;
  278. if (sg_count) {
  279. old_sg = (struct scatterlist *) SCpnt->request_buffer;
  280. old_bbpnt = SCpnt->bounce_buffers;
  281. } else {
  282. sg_count = 1;
  283. sg_ent++;
  284. }
  285. /* Get space for scatterlist and bounce buffer array. */
  286. i  = sg_ent * sizeof(struct scatterlist);
  287. i += sg_ent * sizeof(void *);
  288. i  = (i + 511) & ~511;
  289. if ((sg = scsi_malloc(i)) == NULL)
  290. goto no_mem;
  291. bbpnt = (void **)
  292. ((char *)sg + (sg_ent * sizeof(struct scatterlist)));
  293. /*
  294.  * no more failing memory allocs possible, we can safely assign
  295.  * SCpnt values now
  296.  */
  297. SCpnt->sglist_len = i;
  298. SCpnt->use_sg = sg_count;
  299. memset(sg, 0, SCpnt->sglist_len);
  300. i = 0;
  301. if (fsize) {
  302. sg[0].address = bbpnt[0] = front;
  303. sg[0].length = fsize;
  304. i++;
  305. }
  306. if (old_sg) {
  307. memcpy(sg + i, old_sg, SCpnt->use_sg * sizeof(struct scatterlist));
  308. if (old_bbpnt)
  309. memcpy(bbpnt + i, old_bbpnt, SCpnt->use_sg * sizeof(void *));
  310. scsi_free(old_sg, (((SCpnt->use_sg * sizeof(struct scatterlist)) +
  311.     (SCpnt->use_sg * sizeof(void *))) + 511) & ~511);
  312. } else {
  313. sg[i].address = SCpnt->request_buffer;
  314. sg[i].length = SCpnt->request_bufflen;
  315. }
  316. SCpnt->request_bufflen += (fsize + bsize);
  317. SCpnt->request_buffer = sg;
  318. SCpnt->bounce_buffers = bbpnt;
  319. SCpnt->use_sg += i;
  320. if (bsize) {
  321. sg[SCpnt->use_sg].address = back;
  322. bbpnt[SCpnt->use_sg] = back;
  323. sg[SCpnt->use_sg].length = bsize;
  324. SCpnt->use_sg++;
  325. }
  326. return 0;
  327. no_mem:
  328. printk("sr: ran out of mem for scatter padn");
  329. if (front)
  330. scsi_free(front, fsize);
  331. if (back)
  332. scsi_free(back, bsize);
  333. return 1;
  334. }
  335. static int sr_init_command(Scsi_Cmnd * SCpnt)
  336. {
  337. int dev, devm, block=0, this_count, s_size;
  338. devm = MINOR(SCpnt->request.rq_dev);
  339. dev = DEVICE_NR(SCpnt->request.rq_dev);
  340. SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %d, block = %dn", devm, block));
  341. if (dev >= sr_template.nr_dev ||
  342.     !scsi_CDs[dev].device ||
  343.     !scsi_CDs[dev].device->online) {
  344. SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectorsn", SCpnt->request.nr_sectors));
  345. SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%pn", SCpnt));
  346. return 0;
  347. }
  348. if (scsi_CDs[dev].device->changed) {
  349. /*
  350.  * quietly refuse to do anything to a changed disc until the
  351.  * changed bit has been reset
  352.  */
  353. return 0;
  354. }
  355. if ((SCpnt->request.cmd == WRITE) && !scsi_CDs[dev].device->writeable)
  356. return 0;
  357. /*
  358.  * we do lazy blocksize switching (when reading XA sectors,
  359.  * see CDROMREADMODE2 ioctl) 
  360.  */
  361. s_size = scsi_CDs[dev].device->sector_size;
  362. if (s_size > 2048) {
  363. if (!in_interrupt())
  364. sr_set_blocklength(DEVICE_NR(CURRENT->rq_dev), 2048);
  365. else
  366. printk("sr: can't switch blocksize: in interruptn");
  367. }
  368. if (s_size != 512 && s_size != 1024 && s_size != 2048) {
  369. printk("sr: bad sector size %dn", s_size);
  370. return 0;
  371. }
  372. block = SCpnt->request.sector / (s_size >> 9);
  373. /*
  374.  * request doesn't start on hw block boundary, add scatter pads
  375.  */
  376. if ((SCpnt->request.sector % (s_size >> 9)) || (SCpnt->request_bufflen % s_size))
  377. if (sr_scatter_pad(SCpnt, s_size))
  378. return 0;
  379. this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
  380. switch (SCpnt->request.cmd) {
  381. case WRITE:
  382. SCpnt->cmnd[0] = WRITE_10;
  383. SCpnt->sc_data_direction = SCSI_DATA_WRITE;
  384. break;
  385. case READ:
  386. SCpnt->cmnd[0] = READ_10;
  387. SCpnt->sc_data_direction = SCSI_DATA_READ;
  388. break;
  389. default:
  390. printk("Unknown sr command %dn", SCpnt->request.cmd);
  391. return 0;
  392. }
  393. SCSI_LOG_HLQUEUE(2, printk("sr%d : %s %d/%ld 512 byte blocks.n",
  394.                                    devm,
  395.    (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
  396.  this_count, SCpnt->request.nr_sectors));
  397. SCpnt->cmnd[1] = (SCpnt->device->scsi_level <= SCSI_2) ?
  398.  ((SCpnt->lun << 5) & 0xe0) : 0;
  399. if (this_count > 0xffff)
  400. this_count = 0xffff;
  401. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  402. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  403. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  404. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  405. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  406. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  407. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  408. /*
  409.  * We shouldn't disconnect in the middle of a sector, so with a dumb
  410.  * host adapter, it's safe to assume that we can at least transfer
  411.  * this many bytes between each connect / disconnect.
  412.  */
  413. SCpnt->transfersize = scsi_CDs[dev].device->sector_size;
  414. SCpnt->underflow = this_count << 9;
  415. SCpnt->allowed = MAX_RETRIES;
  416. SCpnt->timeout_per_command = SR_TIMEOUT;
  417. /*
  418.  * This is the completion routine we use.  This is matched in terms
  419.  * of capability to this function.
  420.  */
  421. SCpnt->done = rw_intr;
  422. {
  423. struct scatterlist *sg = SCpnt->request_buffer;
  424. int i, size = 0;
  425. for (i = 0; i < SCpnt->use_sg; i++)
  426. size += sg[i].length;
  427. if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
  428. printk("sr: mismatch count %d, bytes %dn", size, SCpnt->request_bufflen);
  429. SCpnt->request_bufflen = size;
  430. }
  431. }
  432. /*
  433.  * This indicates that the command is ready from our end to be
  434.  * queued.
  435.  */
  436. return 1;
  437. }
  438. struct block_device_operations sr_bdops =
  439. {
  440. owner: THIS_MODULE,
  441. open: cdrom_open,
  442. release: cdrom_release,
  443. ioctl: cdrom_ioctl,
  444. check_media_change: cdrom_media_changed,
  445. };
  446. static int sr_open(struct cdrom_device_info *cdi, int purpose)
  447. {
  448. check_disk_change(cdi->dev);
  449. if (MINOR(cdi->dev) >= sr_template.dev_max
  450.     || !scsi_CDs[MINOR(cdi->dev)].device) {
  451. return -ENXIO; /* No such device */
  452. }
  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. if (!scsi_block_when_processing_errors(scsi_CDs[MINOR(cdi->dev)].device)) {
  458. return -ENXIO;
  459. }
  460. scsi_CDs[MINOR(cdi->dev)].device->access_count++;
  461. if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
  462. __MOD_INC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
  463. if (sr_template.module)
  464. __MOD_INC_USE_COUNT(sr_template.module);
  465. /* If this device did not have media in the drive at boot time, then
  466.  * we would have been unable to get the sector size.  Check to see if
  467.  * this is the case, and try again.
  468.  */
  469. if (scsi_CDs[MINOR(cdi->dev)].needs_sector_size)
  470. get_sectorsize(MINOR(cdi->dev));
  471. return 0;
  472. }
  473. static int sr_detect(Scsi_Device * SDp)
  474. {
  475. if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
  476. return 0;
  477. sr_template.dev_noticed++;
  478. return 1;
  479. }
  480. static int sr_attach(Scsi_Device * SDp)
  481. {
  482. Scsi_CD *cpnt;
  483. int i;
  484. if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
  485. return 1;
  486. if (sr_template.nr_dev >= sr_template.dev_max) {
  487. SDp->attached--;
  488. return 1;
  489. }
  490. for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
  491. if (!cpnt->device)
  492. break;
  493. if (i >= sr_template.dev_max)
  494. panic("scsi_devices corrupt (sr)");
  495. scsi_CDs[i].device = SDp;
  496. sr_template.nr_dev++;
  497. if (sr_template.nr_dev > sr_template.dev_max)
  498. panic("scsi_devices corrupt (sr)");
  499. printk("Attached scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %dn",
  500.        i, SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
  501. return 0;
  502. }
  503. void get_sectorsize(int i)
  504. {
  505. unsigned char cmd[10];
  506. unsigned char *buffer;
  507. int the_result, retries;
  508. int sector_size;
  509. Scsi_Request *SRpnt;
  510. buffer = (unsigned char *) scsi_malloc(512);
  511. SRpnt = scsi_allocate_request(scsi_CDs[i].device);
  512. if(buffer == NULL || SRpnt == NULL)
  513. {
  514. scsi_CDs[i].capacity = 0x1fffff;
  515. sector_size = 2048; /* A guess, just in case */
  516. scsi_CDs[i].needs_sector_size = 1;
  517. if(buffer)
  518. scsi_free(buffer, 512);
  519. if(SRpnt)
  520. scsi_release_request(SRpnt);
  521. return;
  522. }
  523. retries = 3;
  524. do {
  525. cmd[0] = READ_CAPACITY;
  526. cmd[1] = (scsi_CDs[i].device->scsi_level <= SCSI_2) ?
  527.  ((scsi_CDs[i].device->lun << 5) & 0xe0) : 0;
  528. memset((void *) &cmd[2], 0, 8);
  529. SRpnt->sr_request.rq_status = RQ_SCSI_BUSY; /* Mark as really busy */
  530. SRpnt->sr_cmd_len = 0;
  531. memset(buffer, 0, 8);
  532. /* Do the command and wait.. */
  533. SRpnt->sr_data_direction = SCSI_DATA_READ;
  534. scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
  535.       8, SR_TIMEOUT, MAX_RETRIES);
  536. the_result = SRpnt->sr_result;
  537. retries--;
  538. } while (the_result && retries);
  539. scsi_release_request(SRpnt);
  540. SRpnt = NULL;
  541. if (the_result) {
  542. scsi_CDs[i].capacity = 0x1fffff;
  543. sector_size = 2048; /* A guess, just in case */
  544. scsi_CDs[i].needs_sector_size = 1;
  545. } else {
  546. #if 0
  547. if (cdrom_get_last_written(MKDEV(MAJOR_NR, i),
  548.    &scsi_CDs[i].capacity))
  549. #endif
  550. scsi_CDs[i].capacity = 1 + ((buffer[0] << 24) |
  551.     (buffer[1] << 16) |
  552.     (buffer[2] << 8) |
  553.     buffer[3]);
  554. sector_size = (buffer[4] << 24) |
  555.     (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  556. switch (sector_size) {
  557. /*
  558.  * HP 4020i CD-Recorder reports 2340 byte sectors
  559.  * Philips CD-Writers report 2352 byte sectors
  560.  *
  561.  * Use 2k sectors for them..
  562.  */
  563. case 0:
  564. case 2340:
  565. case 2352:
  566. sector_size = 2048;
  567. /* fall through */
  568. case 2048:
  569. scsi_CDs[i].capacity *= 4;
  570. /* fall through */
  571. case 512:
  572. break;
  573. default:
  574. printk("sr%d: unsupported sector size %d.n",
  575.        i, sector_size);
  576. scsi_CDs[i].capacity = 0;
  577. scsi_CDs[i].needs_sector_size = 1;
  578. }
  579. scsi_CDs[i].device->sector_size = sector_size;
  580. /*
  581.  * Add this so that we have the ability to correctly gauge
  582.  * what the device is capable of.
  583.  */
  584. scsi_CDs[i].needs_sector_size = 0;
  585. sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
  586. };
  587. scsi_free(buffer, 512);
  588. }
  589. void get_capabilities(int i)
  590. {
  591. unsigned char cmd[6];
  592. unsigned char *buffer;
  593. int rc, n;
  594. static char *loadmech[] =
  595. {
  596. "caddy",
  597. "tray",
  598. "pop-up",
  599. "",
  600. "changer",
  601. "cartridge changer",
  602. "",
  603. ""
  604. };
  605. buffer = (unsigned char *) scsi_malloc(512);
  606. if (!buffer)
  607. {
  608. printk(KERN_ERR "sr: out of memory.n");
  609. return;
  610. }
  611. cmd[0] = MODE_SENSE;
  612. cmd[1] = (scsi_CDs[i].device->scsi_level <= SCSI_2) ?
  613.  ((scsi_CDs[i].device->lun << 5) & 0xe0) : 0;
  614. cmd[2] = 0x2a;
  615. cmd[4] = 128;
  616. cmd[3] = cmd[5] = 0;
  617. rc = sr_do_ioctl(i, cmd, buffer, 128, 1, SCSI_DATA_READ, NULL);
  618. if (rc) {
  619. /* failed, drive doesn't have capabilities mode page */
  620. scsi_CDs[i].cdi.speed = 1;
  621. scsi_CDs[i].cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
  622.  CDC_DVD | CDC_DVD_RAM |
  623.  CDC_SELECT_DISC | CDC_SELECT_SPEED);
  624. scsi_free(buffer, 512);
  625. printk("sr%i: scsi-1 driven", i);
  626. return;
  627. }
  628. n = buffer[3] + 4;
  629. scsi_CDs[i].cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
  630. scsi_CDs[i].readcd_known = 1;
  631. scsi_CDs[i].readcd_cdda = buffer[n + 5] & 0x01;
  632. /* print some capability bits */
  633. printk("sr%i: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%sn", i,
  634.        ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
  635.        scsi_CDs[i].cdi.speed,
  636.        buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
  637.        buffer[n + 3] & 0x20 ? "dvd-ram " : "",
  638.        buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
  639.        buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
  640.        buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
  641.        loadmech[buffer[n + 6] >> 5]);
  642. if ((buffer[n + 6] >> 5) == 0)
  643. /* caddy drives can't close tray... */
  644. scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY;
  645. if ((buffer[n + 2] & 0x8) == 0)
  646. /* not a DVD drive */
  647. scsi_CDs[i].cdi.mask |= CDC_DVD;
  648. if ((buffer[n + 3] & 0x20) == 0) {
  649. /* can't write DVD-RAM media */
  650. scsi_CDs[i].cdi.mask |= CDC_DVD_RAM;
  651. } else {
  652. scsi_CDs[i].device->writeable = 1;
  653. }
  654. if ((buffer[n + 3] & 0x10) == 0)
  655. /* can't write DVD-R media */
  656. scsi_CDs[i].cdi.mask |= CDC_DVD_R;
  657. if ((buffer[n + 3] & 0x2) == 0)
  658. /* can't write CD-RW media */
  659. scsi_CDs[i].cdi.mask |= CDC_CD_RW;
  660. if ((buffer[n + 3] & 0x1) == 0)
  661. /* can't write CD-R media */
  662. scsi_CDs[i].cdi.mask |= CDC_CD_R;
  663. if ((buffer[n + 6] & 0x8) == 0)
  664. /* can't eject */
  665. scsi_CDs[i].cdi.mask |= CDC_OPEN_TRAY;
  666. if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
  667.     (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
  668. scsi_CDs[i].cdi.capacity =
  669.     cdrom_number_of_slots(&(scsi_CDs[i].cdi));
  670. if (scsi_CDs[i].cdi.capacity <= 1)
  671. /* not a changer */
  672. scsi_CDs[i].cdi.mask |= CDC_SELECT_DISC;
  673. /*else    I don't think it can close its tray
  674.    scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY; */
  675. scsi_free(buffer, 512);
  676. }
  677. /*
  678.  * sr_packet() is the entry point for the generic commands generated
  679.  * by the Uniform CD-ROM layer. 
  680.  */
  681. static int sr_packet(struct cdrom_device_info *cdi, struct cdrom_generic_command *cgc)
  682. {
  683. Scsi_Device *device = scsi_CDs[MINOR(cdi->dev)].device;
  684. /* set the LUN */
  685. if (device->scsi_level <= SCSI_2)
  686. cgc->cmd[1] |= device->lun << 5;
  687. cgc->stat = sr_do_ioctl(MINOR(cdi->dev), cgc->cmd, cgc->buffer, cgc->buflen, cgc->quiet, cgc->data_direction, cgc->sense);
  688. return cgc->stat;
  689. }
  690. static int sr_registered;
  691. static int sr_init()
  692. {
  693. int i;
  694. if (sr_template.dev_noticed == 0)
  695. return 0;
  696. if (!sr_registered) {
  697. if (devfs_register_blkdev(MAJOR_NR, "sr", &sr_bdops)) {
  698. printk("Unable to get major %d for SCSI-CDn", MAJOR_NR);
  699. sr_template.dev_noticed = 0;
  700. return 1;
  701. }
  702. sr_registered++;
  703. }
  704. if (scsi_CDs)
  705. return 0;
  706. sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
  707. scsi_CDs = kmalloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
  708. if (!scsi_CDs)
  709. goto cleanup_devfs;
  710. memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
  711. sr_sizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
  712. if (!sr_sizes)
  713. goto cleanup_cds;
  714. memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
  715. sr_blocksizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
  716. if (!sr_blocksizes)
  717. goto cleanup_sizes;
  718. sr_hardsizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
  719. if (!sr_hardsizes)
  720. goto cleanup_blocksizes;
  721. /*
  722.  * These are good guesses for the time being.
  723.  */
  724. for (i = 0; i < sr_template.dev_max; i++) {
  725. sr_blocksizes[i] = 2048;
  726. sr_hardsizes[i] = 2048;
  727.         }
  728. blksize_size[MAJOR_NR] = sr_blocksizes;
  729.         hardsect_size[MAJOR_NR] = sr_hardsizes;
  730. return 0;
  731. cleanup_blocksizes:
  732. kfree(sr_blocksizes);
  733. cleanup_sizes:
  734. kfree(sr_sizes);
  735. cleanup_cds:
  736. kfree(scsi_CDs);
  737. scsi_CDs = NULL;
  738. cleanup_devfs:
  739. devfs_unregister_blkdev(MAJOR_NR, "sr");
  740. sr_template.dev_noticed = 0;
  741. sr_registered--;
  742. return 1;
  743. }
  744. void sr_finish()
  745. {
  746. int i;
  747. char name[6];
  748. blk_dev[MAJOR_NR].queue = sr_find_queue;
  749. blk_size[MAJOR_NR] = sr_sizes;
  750. for (i = 0; i < sr_template.nr_dev; ++i) {
  751. /* If we have already seen this, then skip it.  Comes up
  752.  * with loadable modules. */
  753. if (scsi_CDs[i].capacity)
  754. continue;
  755. scsi_CDs[i].capacity = 0x1fffff;
  756. scsi_CDs[i].device->sector_size = 2048; /* A guess, just in case */
  757. scsi_CDs[i].needs_sector_size = 1;
  758. scsi_CDs[i].device->changed = 1; /* force recheck CD type */
  759. #if 0
  760. /* seems better to leave this for later */
  761. get_sectorsize(i);
  762. printk("Scd sectorsize = %d bytes.n", scsi_CDs[i].sector_size);
  763. #endif
  764. scsi_CDs[i].use = 1;
  765. scsi_CDs[i].device->ten = 1;
  766. scsi_CDs[i].device->remap = 1;
  767. scsi_CDs[i].readcd_known = 0;
  768. scsi_CDs[i].readcd_cdda = 0;
  769. sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
  770. scsi_CDs[i].cdi.ops = &sr_dops;
  771. scsi_CDs[i].cdi.handle = &scsi_CDs[i];
  772. scsi_CDs[i].cdi.dev = MKDEV(MAJOR_NR, i);
  773. scsi_CDs[i].cdi.mask = 0;
  774. scsi_CDs[i].cdi.capacity = 1;
  775. /*
  776.  * FIXME: someone needs to handle a get_capabilities
  777.  * failure properly ??
  778.  */
  779. get_capabilities(i);
  780. sr_vendor_init(i);
  781. sprintf(name, "sr%d", i);
  782. strcpy(scsi_CDs[i].cdi.name, name);
  783.                 scsi_CDs[i].cdi.de =
  784.                     devfs_register (scsi_CDs[i].device->de, "cd",
  785.                                     DEVFS_FL_DEFAULT, MAJOR_NR, i,
  786.                                     S_IFBLK | S_IRUGO | S_IWUGO,
  787.                                     &sr_bdops, NULL);
  788. register_cdrom(&scsi_CDs[i].cdi);
  789. }
  790. /* If our host adapter is capable of scatter-gather, then we increase
  791.  * the read-ahead to 16 blocks (32 sectors).  If not, we use
  792.  * a two block (4 sector) read ahead. */
  793. if (scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
  794. read_ahead[MAJOR_NR] = 32; /* 32 sector read-ahead.  Always removable. */
  795. else
  796. read_ahead[MAJOR_NR] = 4; /* 4 sector read-ahead */
  797. return;
  798. }
  799. static void sr_detach(Scsi_Device * SDp)
  800. {
  801. Scsi_CD *cpnt;
  802. int i;
  803. if (scsi_CDs == NULL)
  804. return;
  805. for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
  806. if (cpnt->device == SDp) {
  807. /*
  808.  * Since the cdrom is read-only, no need to sync the device.
  809.  * We should be kind to our buffer cache, however.
  810.  */
  811. invalidate_device(MKDEV(MAJOR_NR, i), 0);
  812. /*
  813.  * Reset things back to a sane state so that one can re-load a new
  814.  * driver (perhaps the same one).
  815.  */
  816. unregister_cdrom(&(cpnt->cdi));
  817. cpnt->device = NULL;
  818. cpnt->capacity = 0;
  819. SDp->attached--;
  820. sr_template.nr_dev--;
  821. sr_template.dev_noticed--;
  822. sr_sizes[i] = 0;
  823. return;
  824. }
  825. return;
  826. }
  827. static int __init init_sr(void)
  828. {
  829. sr_template.module = THIS_MODULE;
  830. return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
  831. }
  832. static void __exit exit_sr(void)
  833. {
  834. scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
  835. devfs_unregister_blkdev(MAJOR_NR, "sr");
  836. sr_registered--;
  837. if (scsi_CDs != NULL) {
  838. kfree(scsi_CDs);
  839. kfree(sr_sizes);
  840. sr_sizes = NULL;
  841. kfree(sr_blocksizes);
  842. sr_blocksizes = NULL;
  843. kfree(sr_hardsizes);
  844. sr_hardsizes = NULL;
  845. }
  846. blksize_size[MAJOR_NR] = NULL;
  847.         hardsect_size[MAJOR_NR] = NULL;
  848. blk_size[MAJOR_NR] = NULL;
  849. read_ahead[MAJOR_NR] = 0;
  850. sr_template.dev_max = 0;
  851. }
  852. module_init(init_sr);
  853. module_exit(exit_sr);
  854. MODULE_LICENSE("GPL");