sr.c 26 KB

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