sr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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/mm.h>
  38. #include <linux/bio.h>
  39. #include <linux/string.h>
  40. #include <linux/errno.h>
  41. #include <linux/cdrom.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/init.h>
  44. #include <linux/blkdev.h>
  45. #include <linux/mutex.h>
  46. #include <linux/slab.h>
  47. #include <asm/uaccess.h>
  48. #include <scsi/scsi.h>
  49. #include <scsi/scsi_dbg.h>
  50. #include <scsi/scsi_device.h>
  51. #include <scsi/scsi_driver.h>
  52. #include <scsi/scsi_cmnd.h>
  53. #include <scsi/scsi_eh.h>
  54. #include <scsi/scsi_host.h>
  55. #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
  56. #include "scsi_logging.h"
  57. #include "sr.h"
  58. MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
  59. MODULE_LICENSE("GPL");
  60. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
  61. MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
  62. MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
  63. #define SR_DISKS 256
  64. #define SR_CAPABILITIES \
  65. (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
  66. CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
  67. CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
  68. CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
  69. CDC_MRW|CDC_MRW_W|CDC_RAM)
  70. static DEFINE_MUTEX(sr_mutex);
  71. static int sr_probe(struct device *);
  72. static int sr_remove(struct device *);
  73. static int sr_done(struct scsi_cmnd *);
  74. static struct scsi_driver sr_template = {
  75. .owner = THIS_MODULE,
  76. .gendrv = {
  77. .name = "sr",
  78. .probe = sr_probe,
  79. .remove = sr_remove,
  80. },
  81. .done = sr_done,
  82. };
  83. static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
  84. static DEFINE_SPINLOCK(sr_index_lock);
  85. /* This semaphore is used to mediate the 0->1 reference get in the
  86. * face of object destruction (i.e. we can't allow a get on an
  87. * object after last put) */
  88. static DEFINE_MUTEX(sr_ref_mutex);
  89. static int sr_open(struct cdrom_device_info *, int);
  90. static void sr_release(struct cdrom_device_info *);
  91. static void get_sectorsize(struct scsi_cd *);
  92. static void get_capabilities(struct scsi_cd *);
  93. static unsigned int sr_check_events(struct cdrom_device_info *cdi,
  94. unsigned int clearing, int slot);
  95. static int sr_packet(struct cdrom_device_info *, struct packet_command *);
  96. static struct cdrom_device_ops sr_dops = {
  97. .open = sr_open,
  98. .release = sr_release,
  99. .drive_status = sr_drive_status,
  100. .check_events = sr_check_events,
  101. .tray_move = sr_tray_move,
  102. .lock_door = sr_lock_door,
  103. .select_speed = sr_select_speed,
  104. .get_last_session = sr_get_last_session,
  105. .get_mcn = sr_get_mcn,
  106. .reset = sr_reset,
  107. .audio_ioctl = sr_audio_ioctl,
  108. .capability = SR_CAPABILITIES,
  109. .generic_packet = sr_packet,
  110. };
  111. static void sr_kref_release(struct kref *kref);
  112. static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
  113. {
  114. return container_of(disk->private_data, struct scsi_cd, driver);
  115. }
  116. /*
  117. * The get and put routines for the struct scsi_cd. Note this entity
  118. * has a scsi_device pointer and owns a reference to this.
  119. */
  120. static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
  121. {
  122. struct scsi_cd *cd = NULL;
  123. mutex_lock(&sr_ref_mutex);
  124. if (disk->private_data == NULL)
  125. goto out;
  126. cd = scsi_cd(disk);
  127. kref_get(&cd->kref);
  128. if (scsi_device_get(cd->device))
  129. goto out_put;
  130. goto out;
  131. out_put:
  132. kref_put(&cd->kref, sr_kref_release);
  133. cd = NULL;
  134. out:
  135. mutex_unlock(&sr_ref_mutex);
  136. return cd;
  137. }
  138. static void scsi_cd_put(struct scsi_cd *cd)
  139. {
  140. struct scsi_device *sdev = cd->device;
  141. mutex_lock(&sr_ref_mutex);
  142. kref_put(&cd->kref, sr_kref_release);
  143. scsi_device_put(sdev);
  144. mutex_unlock(&sr_ref_mutex);
  145. }
  146. static unsigned int sr_get_events(struct scsi_device *sdev)
  147. {
  148. u8 buf[8];
  149. u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION,
  150. 1, /* polled */
  151. 0, 0, /* reserved */
  152. 1 << 4, /* notification class: media */
  153. 0, 0, /* reserved */
  154. 0, sizeof(buf), /* allocation length */
  155. 0, /* control */
  156. };
  157. struct event_header *eh = (void *)buf;
  158. struct media_event_desc *med = (void *)(buf + 4);
  159. struct scsi_sense_hdr sshdr;
  160. int result;
  161. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf),
  162. &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL);
  163. if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION)
  164. return DISK_EVENT_MEDIA_CHANGE;
  165. if (result || be16_to_cpu(eh->data_len) < sizeof(*med))
  166. return 0;
  167. if (eh->nea || eh->notification_class != 0x4)
  168. return 0;
  169. if (med->media_event_code == 1)
  170. return DISK_EVENT_EJECT_REQUEST;
  171. else if (med->media_event_code == 2)
  172. return DISK_EVENT_MEDIA_CHANGE;
  173. return 0;
  174. }
  175. /*
  176. * This function checks to see if the media has been changed or eject
  177. * button has been pressed. It is possible that we have already
  178. * sensed a change, or the drive may have sensed one and not yet
  179. * reported it. The past events are accumulated in sdev->changed and
  180. * returned together with the current state.
  181. */
  182. static unsigned int sr_check_events(struct cdrom_device_info *cdi,
  183. unsigned int clearing, int slot)
  184. {
  185. struct scsi_cd *cd = cdi->handle;
  186. bool last_present;
  187. struct scsi_sense_hdr sshdr;
  188. unsigned int events;
  189. int ret;
  190. /* no changer support */
  191. if (CDSL_CURRENT != slot)
  192. return 0;
  193. events = sr_get_events(cd->device);
  194. /*
  195. * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE
  196. * is being cleared. Note that there are devices which hang
  197. * if asked to execute TUR repeatedly.
  198. */
  199. if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
  200. goto skip_tur;
  201. /* let's see whether the media is there with TUR */
  202. last_present = cd->media_present;
  203. ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
  204. /*
  205. * Media is considered to be present if TUR succeeds or fails with
  206. * sense data indicating something other than media-not-present
  207. * (ASC 0x3a).
  208. */
  209. cd->media_present = scsi_status_is_good(ret) ||
  210. (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
  211. if (last_present != cd->media_present)
  212. events |= DISK_EVENT_MEDIA_CHANGE;
  213. skip_tur:
  214. if (cd->device->changed) {
  215. events |= DISK_EVENT_MEDIA_CHANGE;
  216. cd->device->changed = 0;
  217. }
  218. return events;
  219. }
  220. /*
  221. * sr_done is the interrupt routine for the device driver.
  222. *
  223. * It will be notified on the end of a SCSI read / write, and will take one
  224. * of several actions based on success or failure.
  225. */
  226. static int sr_done(struct scsi_cmnd *SCpnt)
  227. {
  228. int result = SCpnt->result;
  229. int this_count = scsi_bufflen(SCpnt);
  230. int good_bytes = (result == 0 ? this_count : 0);
  231. int block_sectors = 0;
  232. long error_sector;
  233. struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
  234. #ifdef DEBUG
  235. printk("sr.c done: %x\n", result);
  236. #endif
  237. /*
  238. * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
  239. * success. Since this is a relatively rare error condition, no
  240. * care is taken to avoid unnecessary additional work such as
  241. * memcpy's that could be avoided.
  242. */
  243. if (driver_byte(result) != 0 && /* An error occurred */
  244. (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
  245. switch (SCpnt->sense_buffer[2]) {
  246. case MEDIUM_ERROR:
  247. case VOLUME_OVERFLOW:
  248. case ILLEGAL_REQUEST:
  249. if (!(SCpnt->sense_buffer[0] & 0x90))
  250. break;
  251. error_sector = (SCpnt->sense_buffer[3] << 24) |
  252. (SCpnt->sense_buffer[4] << 16) |
  253. (SCpnt->sense_buffer[5] << 8) |
  254. SCpnt->sense_buffer[6];
  255. if (SCpnt->request->bio != NULL)
  256. block_sectors =
  257. bio_sectors(SCpnt->request->bio);
  258. if (block_sectors < 4)
  259. block_sectors = 4;
  260. if (cd->device->sector_size == 2048)
  261. error_sector <<= 2;
  262. error_sector &= ~(block_sectors - 1);
  263. good_bytes = (error_sector -
  264. blk_rq_pos(SCpnt->request)) << 9;
  265. if (good_bytes < 0 || good_bytes >= this_count)
  266. good_bytes = 0;
  267. /*
  268. * The SCSI specification allows for the value
  269. * returned by READ CAPACITY to be up to 75 2K
  270. * sectors past the last readable block.
  271. * Therefore, if we hit a medium error within the
  272. * last 75 2K sectors, we decrease the saved size
  273. * value.
  274. */
  275. if (error_sector < get_capacity(cd->disk) &&
  276. cd->capacity - error_sector < 4 * 75)
  277. set_capacity(cd->disk, error_sector);
  278. break;
  279. case RECOVERED_ERROR:
  280. good_bytes = this_count;
  281. break;
  282. default:
  283. break;
  284. }
  285. }
  286. return good_bytes;
  287. }
  288. static int sr_prep_fn(struct request_queue *q, struct request *rq)
  289. {
  290. int block = 0, this_count, s_size;
  291. struct scsi_cd *cd;
  292. struct scsi_cmnd *SCpnt;
  293. struct scsi_device *sdp = q->queuedata;
  294. int ret;
  295. if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  296. ret = scsi_setup_blk_pc_cmnd(sdp, rq);
  297. goto out;
  298. } else if (rq->cmd_type != REQ_TYPE_FS) {
  299. ret = BLKPREP_KILL;
  300. goto out;
  301. }
  302. ret = scsi_setup_fs_cmnd(sdp, rq);
  303. if (ret != BLKPREP_OK)
  304. goto out;
  305. SCpnt = rq->special;
  306. cd = scsi_cd(rq->rq_disk);
  307. /* from here on until we're complete, any goto out
  308. * is used for a killable error condition */
  309. ret = BLKPREP_KILL;
  310. SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
  311. cd->disk->disk_name, block));
  312. if (!cd->device || !scsi_device_online(cd->device)) {
  313. SCSI_LOG_HLQUEUE(2, printk("Finishing %u sectors\n",
  314. blk_rq_sectors(rq)));
  315. SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
  316. goto out;
  317. }
  318. if (cd->device->changed) {
  319. /*
  320. * quietly refuse to do anything to a changed disc until the
  321. * changed bit has been reset
  322. */
  323. goto out;
  324. }
  325. /*
  326. * we do lazy blocksize switching (when reading XA sectors,
  327. * see CDROMREADMODE2 ioctl)
  328. */
  329. s_size = cd->device->sector_size;
  330. if (s_size > 2048) {
  331. if (!in_interrupt())
  332. sr_set_blocklength(cd, 2048);
  333. else
  334. printk("sr: can't switch blocksize: in interrupt\n");
  335. }
  336. if (s_size != 512 && s_size != 1024 && s_size != 2048) {
  337. scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
  338. goto out;
  339. }
  340. if (rq_data_dir(rq) == WRITE) {
  341. if (!cd->device->writeable)
  342. goto out;
  343. SCpnt->cmnd[0] = WRITE_10;
  344. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  345. cd->cdi.media_written = 1;
  346. } else if (rq_data_dir(rq) == READ) {
  347. SCpnt->cmnd[0] = READ_10;
  348. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  349. } else {
  350. blk_dump_rq_flags(rq, "Unknown sr command");
  351. goto out;
  352. }
  353. {
  354. struct scatterlist *sg;
  355. int i, size = 0, sg_count = scsi_sg_count(SCpnt);
  356. scsi_for_each_sg(SCpnt, sg, sg_count, i)
  357. size += sg->length;
  358. if (size != scsi_bufflen(SCpnt)) {
  359. scmd_printk(KERN_ERR, SCpnt,
  360. "mismatch count %d, bytes %d\n",
  361. size, scsi_bufflen(SCpnt));
  362. if (scsi_bufflen(SCpnt) > size)
  363. SCpnt->sdb.length = size;
  364. }
  365. }
  366. /*
  367. * request doesn't start on hw block boundary, add scatter pads
  368. */
  369. if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
  370. (scsi_bufflen(SCpnt) % s_size)) {
  371. scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
  372. goto out;
  373. }
  374. this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
  375. SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%u 512 byte blocks.\n",
  376. cd->cdi.name,
  377. (rq_data_dir(rq) == WRITE) ?
  378. "writing" : "reading",
  379. this_count, blk_rq_sectors(rq)));
  380. SCpnt->cmnd[1] = 0;
  381. block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
  382. if (this_count > 0xffff) {
  383. this_count = 0xffff;
  384. SCpnt->sdb.length = this_count * s_size;
  385. }
  386. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  387. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  388. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  389. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  390. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  391. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  392. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  393. /*
  394. * We shouldn't disconnect in the middle of a sector, so with a dumb
  395. * host adapter, it's safe to assume that we can at least transfer
  396. * this many bytes between each connect / disconnect.
  397. */
  398. SCpnt->transfersize = cd->device->sector_size;
  399. SCpnt->underflow = this_count << 9;
  400. SCpnt->allowed = MAX_RETRIES;
  401. /*
  402. * This indicates that the command is ready from our end to be
  403. * queued.
  404. */
  405. ret = BLKPREP_OK;
  406. out:
  407. return scsi_prep_return(q, rq, ret);
  408. }
  409. static int sr_block_open(struct block_device *bdev, fmode_t mode)
  410. {
  411. struct scsi_cd *cd;
  412. int ret = -ENXIO;
  413. mutex_lock(&sr_mutex);
  414. cd = scsi_cd_get(bdev->bd_disk);
  415. if (cd) {
  416. ret = cdrom_open(&cd->cdi, bdev, mode);
  417. if (ret)
  418. scsi_cd_put(cd);
  419. }
  420. mutex_unlock(&sr_mutex);
  421. return ret;
  422. }
  423. static int sr_block_release(struct gendisk *disk, fmode_t mode)
  424. {
  425. struct scsi_cd *cd = scsi_cd(disk);
  426. mutex_lock(&sr_mutex);
  427. cdrom_release(&cd->cdi, mode);
  428. scsi_cd_put(cd);
  429. mutex_unlock(&sr_mutex);
  430. return 0;
  431. }
  432. static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
  433. unsigned long arg)
  434. {
  435. struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
  436. struct scsi_device *sdev = cd->device;
  437. void __user *argp = (void __user *)arg;
  438. int ret;
  439. mutex_lock(&sr_mutex);
  440. /*
  441. * Send SCSI addressing ioctls directly to mid level, send other
  442. * ioctls to cdrom/block level.
  443. */
  444. switch (cmd) {
  445. case SCSI_IOCTL_GET_IDLUN:
  446. case SCSI_IOCTL_GET_BUS_NUMBER:
  447. ret = scsi_ioctl(sdev, cmd, argp);
  448. goto out;
  449. }
  450. ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
  451. if (ret != -ENOSYS)
  452. goto out;
  453. /*
  454. * ENODEV means that we didn't recognise the ioctl, or that we
  455. * cannot execute it in the current device state. In either
  456. * case fall through to scsi_ioctl, which will return ENDOEV again
  457. * if it doesn't recognise the ioctl
  458. */
  459. ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
  460. (mode & FMODE_NDELAY) != 0);
  461. if (ret != -ENODEV)
  462. goto out;
  463. ret = scsi_ioctl(sdev, cmd, argp);
  464. out:
  465. mutex_unlock(&sr_mutex);
  466. return ret;
  467. }
  468. static unsigned int sr_block_check_events(struct gendisk *disk,
  469. unsigned int clearing)
  470. {
  471. struct scsi_cd *cd = scsi_cd(disk);
  472. return cdrom_check_events(&cd->cdi, clearing);
  473. }
  474. static int sr_block_revalidate_disk(struct gendisk *disk)
  475. {
  476. struct scsi_cd *cd = scsi_cd(disk);
  477. struct scsi_sense_hdr sshdr;
  478. /* if the unit is not ready, nothing more to do */
  479. if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
  480. return 0;
  481. sr_cd_check(&cd->cdi);
  482. get_sectorsize(cd);
  483. return 0;
  484. }
  485. static const struct block_device_operations sr_bdops =
  486. {
  487. .owner = THIS_MODULE,
  488. .open = sr_block_open,
  489. .release = sr_block_release,
  490. .ioctl = sr_block_ioctl,
  491. .check_events = sr_block_check_events,
  492. .revalidate_disk = sr_block_revalidate_disk,
  493. /*
  494. * No compat_ioctl for now because sr_block_ioctl never
  495. * seems to pass arbitary ioctls down to host drivers.
  496. */
  497. };
  498. static int sr_open(struct cdrom_device_info *cdi, int purpose)
  499. {
  500. struct scsi_cd *cd = cdi->handle;
  501. struct scsi_device *sdev = cd->device;
  502. int retval;
  503. /*
  504. * If the device is in error recovery, wait until it is done.
  505. * If the device is offline, then disallow any access to it.
  506. */
  507. retval = -ENXIO;
  508. if (!scsi_block_when_processing_errors(sdev))
  509. goto error_out;
  510. return 0;
  511. error_out:
  512. return retval;
  513. }
  514. static void sr_release(struct cdrom_device_info *cdi)
  515. {
  516. struct scsi_cd *cd = cdi->handle;
  517. if (cd->device->sector_size > 2048)
  518. sr_set_blocklength(cd, 2048);
  519. }
  520. static int sr_probe(struct device *dev)
  521. {
  522. struct scsi_device *sdev = to_scsi_device(dev);
  523. struct gendisk *disk;
  524. struct scsi_cd *cd;
  525. int minor, error;
  526. error = -ENODEV;
  527. if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
  528. goto fail;
  529. error = -ENOMEM;
  530. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  531. if (!cd)
  532. goto fail;
  533. kref_init(&cd->kref);
  534. disk = alloc_disk(1);
  535. if (!disk)
  536. goto fail_free;
  537. spin_lock(&sr_index_lock);
  538. minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
  539. if (minor == SR_DISKS) {
  540. spin_unlock(&sr_index_lock);
  541. error = -EBUSY;
  542. goto fail_put;
  543. }
  544. __set_bit(minor, sr_index_bits);
  545. spin_unlock(&sr_index_lock);
  546. disk->major = SCSI_CDROM_MAJOR;
  547. disk->first_minor = minor;
  548. sprintf(disk->disk_name, "sr%d", minor);
  549. disk->fops = &sr_bdops;
  550. disk->flags = GENHD_FL_CD;
  551. disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
  552. blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
  553. cd->device = sdev;
  554. cd->disk = disk;
  555. cd->driver = &sr_template;
  556. cd->disk = disk;
  557. cd->capacity = 0x1fffff;
  558. cd->device->changed = 1; /* force recheck CD type */
  559. cd->media_present = 1;
  560. cd->use = 1;
  561. cd->readcd_known = 0;
  562. cd->readcd_cdda = 0;
  563. cd->cdi.ops = &sr_dops;
  564. cd->cdi.handle = cd;
  565. cd->cdi.mask = 0;
  566. cd->cdi.capacity = 1;
  567. sprintf(cd->cdi.name, "sr%d", minor);
  568. sdev->sector_size = 2048; /* A guess, just in case */
  569. /* FIXME: need to handle a get_capabilities failure properly ?? */
  570. get_capabilities(cd);
  571. blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
  572. sr_vendor_init(cd);
  573. disk->driverfs_dev = &sdev->sdev_gendev;
  574. set_capacity(disk, cd->capacity);
  575. disk->private_data = &cd->driver;
  576. disk->queue = sdev->request_queue;
  577. cd->cdi.disk = disk;
  578. if (register_cdrom(&cd->cdi))
  579. goto fail_put;
  580. dev_set_drvdata(dev, cd);
  581. disk->flags |= GENHD_FL_REMOVABLE;
  582. add_disk(disk);
  583. sdev_printk(KERN_DEBUG, sdev,
  584. "Attached scsi CD-ROM %s\n", cd->cdi.name);
  585. return 0;
  586. fail_put:
  587. put_disk(disk);
  588. fail_free:
  589. kfree(cd);
  590. fail:
  591. return error;
  592. }
  593. static void get_sectorsize(struct scsi_cd *cd)
  594. {
  595. unsigned char cmd[10];
  596. unsigned char buffer[8];
  597. int the_result, retries = 3;
  598. int sector_size;
  599. struct request_queue *queue;
  600. do {
  601. cmd[0] = READ_CAPACITY;
  602. memset((void *) &cmd[1], 0, 9);
  603. memset(buffer, 0, sizeof(buffer));
  604. /* Do the command and wait.. */
  605. the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
  606. buffer, sizeof(buffer), NULL,
  607. SR_TIMEOUT, MAX_RETRIES, NULL);
  608. retries--;
  609. } while (the_result && retries);
  610. if (the_result) {
  611. cd->capacity = 0x1fffff;
  612. sector_size = 2048; /* A guess, just in case */
  613. } else {
  614. long last_written;
  615. cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
  616. (buffer[2] << 8) | buffer[3]);
  617. /*
  618. * READ_CAPACITY doesn't return the correct size on
  619. * certain UDF media. If last_written is larger, use
  620. * it instead.
  621. *
  622. * http://bugzilla.kernel.org/show_bug.cgi?id=9668
  623. */
  624. if (!cdrom_get_last_written(&cd->cdi, &last_written))
  625. cd->capacity = max_t(long, cd->capacity, last_written);
  626. sector_size = (buffer[4] << 24) |
  627. (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  628. switch (sector_size) {
  629. /*
  630. * HP 4020i CD-Recorder reports 2340 byte sectors
  631. * Philips CD-Writers report 2352 byte sectors
  632. *
  633. * Use 2k sectors for them..
  634. */
  635. case 0:
  636. case 2340:
  637. case 2352:
  638. sector_size = 2048;
  639. /* fall through */
  640. case 2048:
  641. cd->capacity *= 4;
  642. /* fall through */
  643. case 512:
  644. break;
  645. default:
  646. printk("%s: unsupported sector size %d.\n",
  647. cd->cdi.name, sector_size);
  648. cd->capacity = 0;
  649. }
  650. cd->device->sector_size = sector_size;
  651. /*
  652. * Add this so that we have the ability to correctly gauge
  653. * what the device is capable of.
  654. */
  655. set_capacity(cd->disk, cd->capacity);
  656. }
  657. queue = cd->device->request_queue;
  658. blk_queue_logical_block_size(queue, sector_size);
  659. return;
  660. }
  661. static void get_capabilities(struct scsi_cd *cd)
  662. {
  663. unsigned char *buffer;
  664. struct scsi_mode_data data;
  665. struct scsi_sense_hdr sshdr;
  666. int rc, n;
  667. static const char *loadmech[] =
  668. {
  669. "caddy",
  670. "tray",
  671. "pop-up",
  672. "",
  673. "changer",
  674. "cartridge changer",
  675. "",
  676. ""
  677. };
  678. /* allocate transfer buffer */
  679. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  680. if (!buffer) {
  681. printk(KERN_ERR "sr: out of memory.\n");
  682. return;
  683. }
  684. /* eat unit attentions */
  685. scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
  686. /* ask for mode page 0x2a */
  687. rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
  688. SR_TIMEOUT, 3, &data, NULL);
  689. if (!scsi_status_is_good(rc)) {
  690. /* failed, drive doesn't have capabilities mode page */
  691. cd->cdi.speed = 1;
  692. cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
  693. CDC_DVD | CDC_DVD_RAM |
  694. CDC_SELECT_DISC | CDC_SELECT_SPEED |
  695. CDC_MRW | CDC_MRW_W | CDC_RAM);
  696. kfree(buffer);
  697. printk("%s: scsi-1 drive\n", cd->cdi.name);
  698. return;
  699. }
  700. n = data.header_length + data.block_descriptor_length;
  701. cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
  702. cd->readcd_known = 1;
  703. cd->readcd_cdda = buffer[n + 5] & 0x01;
  704. /* print some capability bits */
  705. printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
  706. ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
  707. cd->cdi.speed,
  708. buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
  709. buffer[n + 3] & 0x20 ? "dvd-ram " : "",
  710. buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
  711. buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
  712. buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
  713. loadmech[buffer[n + 6] >> 5]);
  714. if ((buffer[n + 6] >> 5) == 0)
  715. /* caddy drives can't close tray... */
  716. cd->cdi.mask |= CDC_CLOSE_TRAY;
  717. if ((buffer[n + 2] & 0x8) == 0)
  718. /* not a DVD drive */
  719. cd->cdi.mask |= CDC_DVD;
  720. if ((buffer[n + 3] & 0x20) == 0)
  721. /* can't write DVD-RAM media */
  722. cd->cdi.mask |= CDC_DVD_RAM;
  723. if ((buffer[n + 3] & 0x10) == 0)
  724. /* can't write DVD-R media */
  725. cd->cdi.mask |= CDC_DVD_R;
  726. if ((buffer[n + 3] & 0x2) == 0)
  727. /* can't write CD-RW media */
  728. cd->cdi.mask |= CDC_CD_RW;
  729. if ((buffer[n + 3] & 0x1) == 0)
  730. /* can't write CD-R media */
  731. cd->cdi.mask |= CDC_CD_R;
  732. if ((buffer[n + 6] & 0x8) == 0)
  733. /* can't eject */
  734. cd->cdi.mask |= CDC_OPEN_TRAY;
  735. if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
  736. (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
  737. cd->cdi.capacity =
  738. cdrom_number_of_slots(&cd->cdi);
  739. if (cd->cdi.capacity <= 1)
  740. /* not a changer */
  741. cd->cdi.mask |= CDC_SELECT_DISC;
  742. /*else I don't think it can close its tray
  743. cd->cdi.mask |= CDC_CLOSE_TRAY; */
  744. /*
  745. * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
  746. */
  747. if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
  748. (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
  749. cd->device->writeable = 1;
  750. }
  751. kfree(buffer);
  752. }
  753. /*
  754. * sr_packet() is the entry point for the generic commands generated
  755. * by the Uniform CD-ROM layer.
  756. */
  757. static int sr_packet(struct cdrom_device_info *cdi,
  758. struct packet_command *cgc)
  759. {
  760. struct scsi_cd *cd = cdi->handle;
  761. struct scsi_device *sdev = cd->device;
  762. if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
  763. return -EDRIVE_CANT_DO_THIS;
  764. if (cgc->timeout <= 0)
  765. cgc->timeout = IOCTL_TIMEOUT;
  766. sr_do_ioctl(cd, cgc);
  767. return cgc->stat;
  768. }
  769. /**
  770. * sr_kref_release - Called to free the scsi_cd structure
  771. * @kref: pointer to embedded kref
  772. *
  773. * sr_ref_mutex must be held entering this routine. Because it is
  774. * called on last put, you should always use the scsi_cd_get()
  775. * scsi_cd_put() helpers which manipulate the semaphore directly
  776. * and never do a direct kref_put().
  777. **/
  778. static void sr_kref_release(struct kref *kref)
  779. {
  780. struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
  781. struct gendisk *disk = cd->disk;
  782. spin_lock(&sr_index_lock);
  783. clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
  784. spin_unlock(&sr_index_lock);
  785. unregister_cdrom(&cd->cdi);
  786. disk->private_data = NULL;
  787. put_disk(disk);
  788. kfree(cd);
  789. }
  790. static int sr_remove(struct device *dev)
  791. {
  792. struct scsi_cd *cd = dev_get_drvdata(dev);
  793. blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
  794. del_gendisk(cd->disk);
  795. mutex_lock(&sr_ref_mutex);
  796. kref_put(&cd->kref, sr_kref_release);
  797. mutex_unlock(&sr_ref_mutex);
  798. return 0;
  799. }
  800. static int __init init_sr(void)
  801. {
  802. int rc;
  803. rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
  804. if (rc)
  805. return rc;
  806. rc = scsi_register_driver(&sr_template.gendrv);
  807. if (rc)
  808. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  809. return rc;
  810. }
  811. static void __exit exit_sr(void)
  812. {
  813. scsi_unregister_driver(&sr_template.gendrv);
  814. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  815. }
  816. module_init(init_sr);
  817. module_exit(exit_sr);
  818. MODULE_LICENSE("GPL");