sr.c 25 KB

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