sr.c 25 KB

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