sr.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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 <[email protected]>
  9. *
  10. * Modified by Eric Youngdale [email protected] to
  11. * add scatter-gather, multiple outstanding request, and other
  12. * enhancements.
  13. *
  14. * Modified by Eric Youngdale [email protected] to support loadable
  15. * low-level scsi drivers.
  16. *
  17. * Modified by Thomas Quinot [email protected] to
  18. * provide auto-eject.
  19. *
  20. * Modified by Gerd Knorr <[email protected]> to support the
  21. * generic cdrom interface
  22. *
  23. * Modified by Jens Axboe <[email protected]> - Uniform sr_packet()
  24. * interface, capabilities probe additions, ioctl cleanups, etc.
  25. *
  26. * Modified by Richard Gooch <[email protected]> to support devfs
  27. *
  28. * Modified by Jens Axboe <[email protected]> - support DVD-RAM
  29. * transparently and lose the GHOST hack
  30. *
  31. * Modified by Arnaldo Carvalho de Melo <[email protected]>
  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 const 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. set_capacity(disk, cd->capacity);
  629. disk->private_data = &cd->driver;
  630. disk->queue = sdev->request_queue;
  631. cd->cdi.disk = disk;
  632. if (register_cdrom(&cd->cdi))
  633. goto fail_put;
  634. /*
  635. * Initialize block layer runtime PM stuffs before the
  636. * periodic event checking request gets started in add_disk.
  637. */
  638. blk_pm_runtime_init(sdev->request_queue, dev);
  639. dev_set_drvdata(dev, cd);
  640. disk->flags |= GENHD_FL_REMOVABLE;
  641. device_add_disk(&sdev->sdev_gendev, disk);
  642. sdev_printk(KERN_DEBUG, sdev,
  643. "Attached scsi CD-ROM %s\n", cd->cdi.name);
  644. scsi_autopm_put_device(cd->device);
  645. return 0;
  646. fail_put:
  647. put_disk(disk);
  648. fail_free:
  649. kfree(cd);
  650. fail:
  651. scsi_autopm_put_device(sdev);
  652. return error;
  653. }
  654. static void get_sectorsize(struct scsi_cd *cd)
  655. {
  656. unsigned char cmd[10];
  657. unsigned char buffer[8];
  658. int the_result, retries = 3;
  659. int sector_size;
  660. struct request_queue *queue;
  661. do {
  662. cmd[0] = READ_CAPACITY;
  663. memset((void *) &cmd[1], 0, 9);
  664. memset(buffer, 0, sizeof(buffer));
  665. /* Do the command and wait.. */
  666. the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
  667. buffer, sizeof(buffer), NULL,
  668. SR_TIMEOUT, MAX_RETRIES, NULL);
  669. retries--;
  670. } while (the_result && retries);
  671. if (the_result) {
  672. cd->capacity = 0x1fffff;
  673. sector_size = 2048; /* A guess, just in case */
  674. } else {
  675. long last_written;
  676. cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
  677. (buffer[2] << 8) | buffer[3]);
  678. /*
  679. * READ_CAPACITY doesn't return the correct size on
  680. * certain UDF media. If last_written is larger, use
  681. * it instead.
  682. *
  683. * http://bugzilla.kernel.org/show_bug.cgi?id=9668
  684. */
  685. if (!cdrom_get_last_written(&cd->cdi, &last_written))
  686. cd->capacity = max_t(long, cd->capacity, last_written);
  687. sector_size = (buffer[4] << 24) |
  688. (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  689. switch (sector_size) {
  690. /*
  691. * HP 4020i CD-Recorder reports 2340 byte sectors
  692. * Philips CD-Writers report 2352 byte sectors
  693. *
  694. * Use 2k sectors for them..
  695. */
  696. case 0:
  697. case 2340:
  698. case 2352:
  699. sector_size = 2048;
  700. /* fall through */
  701. case 2048:
  702. cd->capacity *= 4;
  703. /* fall through */
  704. case 512:
  705. break;
  706. default:
  707. sr_printk(KERN_INFO, cd,
  708. "unsupported sector size %d.", sector_size);
  709. cd->capacity = 0;
  710. }
  711. cd->device->sector_size = sector_size;
  712. /*
  713. * Add this so that we have the ability to correctly gauge
  714. * what the device is capable of.
  715. */
  716. set_capacity(cd->disk, cd->capacity);
  717. }
  718. queue = cd->device->request_queue;
  719. blk_queue_logical_block_size(queue, sector_size);
  720. return;
  721. }
  722. static void get_capabilities(struct scsi_cd *cd)
  723. {
  724. unsigned char *buffer;
  725. struct scsi_mode_data data;
  726. struct scsi_sense_hdr sshdr;
  727. unsigned int ms_len = 128;
  728. int rc, n;
  729. static const char *loadmech[] =
  730. {
  731. "caddy",
  732. "tray",
  733. "pop-up",
  734. "",
  735. "changer",
  736. "cartridge changer",
  737. "",
  738. ""
  739. };
  740. /* allocate transfer buffer */
  741. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  742. if (!buffer) {
  743. sr_printk(KERN_ERR, cd, "out of memory.\n");
  744. return;
  745. }
  746. /* eat unit attentions */
  747. scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
  748. /* ask for mode page 0x2a */
  749. rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len,
  750. SR_TIMEOUT, 3, &data, NULL);
  751. if (!scsi_status_is_good(rc) || data.length > ms_len ||
  752. data.header_length + data.block_descriptor_length > data.length) {
  753. /* failed, drive doesn't have capabilities mode page */
  754. cd->cdi.speed = 1;
  755. cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
  756. CDC_DVD | CDC_DVD_RAM |
  757. CDC_SELECT_DISC | CDC_SELECT_SPEED |
  758. CDC_MRW | CDC_MRW_W | CDC_RAM);
  759. kfree(buffer);
  760. sr_printk(KERN_INFO, cd, "scsi-1 drive");
  761. return;
  762. }
  763. n = data.header_length + data.block_descriptor_length;
  764. cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
  765. cd->readcd_known = 1;
  766. cd->readcd_cdda = buffer[n + 5] & 0x01;
  767. /* print some capability bits */
  768. sr_printk(KERN_INFO, cd,
  769. "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n",
  770. ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
  771. cd->cdi.speed,
  772. buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
  773. buffer[n + 3] & 0x20 ? "dvd-ram " : "",
  774. buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
  775. buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
  776. buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
  777. loadmech[buffer[n + 6] >> 5]);
  778. if ((buffer[n + 6] >> 5) == 0)
  779. /* caddy drives can't close tray... */
  780. cd->cdi.mask |= CDC_CLOSE_TRAY;
  781. if ((buffer[n + 2] & 0x8) == 0)
  782. /* not a DVD drive */
  783. cd->cdi.mask |= CDC_DVD;
  784. if ((buffer[n + 3] & 0x20) == 0)
  785. /* can't write DVD-RAM media */
  786. cd->cdi.mask |= CDC_DVD_RAM;
  787. if ((buffer[n + 3] & 0x10) == 0)
  788. /* can't write DVD-R media */
  789. cd->cdi.mask |= CDC_DVD_R;
  790. if ((buffer[n + 3] & 0x2) == 0)
  791. /* can't write CD-RW media */
  792. cd->cdi.mask |= CDC_CD_RW;
  793. if ((buffer[n + 3] & 0x1) == 0)
  794. /* can't write CD-R media */
  795. cd->cdi.mask |= CDC_CD_R;
  796. if ((buffer[n + 6] & 0x8) == 0)
  797. /* can't eject */
  798. cd->cdi.mask |= CDC_OPEN_TRAY;
  799. if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
  800. (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
  801. cd->cdi.capacity =
  802. cdrom_number_of_slots(&cd->cdi);
  803. if (cd->cdi.capacity <= 1)
  804. /* not a changer */
  805. cd->cdi.mask |= CDC_SELECT_DISC;
  806. /*else I don't think it can close its tray
  807. cd->cdi.mask |= CDC_CLOSE_TRAY; */
  808. /*
  809. * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
  810. */
  811. if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
  812. (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
  813. cd->writeable = 1;
  814. }
  815. kfree(buffer);
  816. }
  817. /*
  818. * sr_packet() is the entry point for the generic commands generated
  819. * by the Uniform CD-ROM layer.
  820. */
  821. static int sr_packet(struct cdrom_device_info *cdi,
  822. struct packet_command *cgc)
  823. {
  824. struct scsi_cd *cd = cdi->handle;
  825. struct scsi_device *sdev = cd->device;
  826. if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
  827. return -EDRIVE_CANT_DO_THIS;
  828. if (cgc->timeout <= 0)
  829. cgc->timeout = IOCTL_TIMEOUT;
  830. sr_do_ioctl(cd, cgc);
  831. return cgc->stat;
  832. }
  833. /**
  834. * sr_kref_release - Called to free the scsi_cd structure
  835. * @kref: pointer to embedded kref
  836. *
  837. * sr_ref_mutex must be held entering this routine. Because it is
  838. * called on last put, you should always use the scsi_cd_get()
  839. * scsi_cd_put() helpers which manipulate the semaphore directly
  840. * and never do a direct kref_put().
  841. **/
  842. static void sr_kref_release(struct kref *kref)
  843. {
  844. struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
  845. struct gendisk *disk = cd->disk;
  846. spin_lock(&sr_index_lock);
  847. clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
  848. spin_unlock(&sr_index_lock);
  849. unregister_cdrom(&cd->cdi);
  850. disk->private_data = NULL;
  851. put_disk(disk);
  852. kfree(cd);
  853. }
  854. static int sr_remove(struct device *dev)
  855. {
  856. struct scsi_cd *cd = dev_get_drvdata(dev);
  857. scsi_autopm_get_device(cd->device);
  858. del_gendisk(cd->disk);
  859. dev_set_drvdata(dev, NULL);
  860. mutex_lock(&sr_ref_mutex);
  861. kref_put(&cd->kref, sr_kref_release);
  862. mutex_unlock(&sr_ref_mutex);
  863. return 0;
  864. }
  865. static int __init init_sr(void)
  866. {
  867. int rc;
  868. rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
  869. if (rc)
  870. return rc;
  871. rc = scsi_register_driver(&sr_template.gendrv);
  872. if (rc)
  873. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  874. return rc;
  875. }
  876. static void __exit exit_sr(void)
  877. {
  878. scsi_unregister_driver(&sr_template.gendrv);
  879. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  880. }
  881. module_init(init_sr);
  882. module_exit(exit_sr);
  883. MODULE_LICENSE("GPL");