sr.c 26 KB

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