sr.c 26 KB

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