sr.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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 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. int ret = -ENXIO;
  454. check_disk_change(bdev);
  455. mutex_lock(&sr_mutex);
  456. cd = scsi_cd_get(bdev->bd_disk);
  457. if (cd) {
  458. ret = cdrom_open(&cd->cdi, bdev, mode);
  459. if (ret)
  460. scsi_cd_put(cd);
  461. }
  462. mutex_unlock(&sr_mutex);
  463. return ret;
  464. }
  465. static void sr_block_release(struct gendisk *disk, fmode_t mode)
  466. {
  467. struct scsi_cd *cd = scsi_cd(disk);
  468. mutex_lock(&sr_mutex);
  469. cdrom_release(&cd->cdi, mode);
  470. scsi_cd_put(cd);
  471. mutex_unlock(&sr_mutex);
  472. }
  473. static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
  474. unsigned long arg)
  475. {
  476. struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
  477. struct scsi_device *sdev = cd->device;
  478. void __user *argp = (void __user *)arg;
  479. int ret;
  480. mutex_lock(&sr_mutex);
  481. ret = scsi_ioctl_block_when_processing_errors(sdev, cmd,
  482. (mode & FMODE_NDELAY) != 0);
  483. if (ret)
  484. goto out;
  485. /*
  486. * Send SCSI addressing ioctls directly to mid level, send other
  487. * ioctls to cdrom/block level.
  488. */
  489. switch (cmd) {
  490. case SCSI_IOCTL_GET_IDLUN:
  491. case SCSI_IOCTL_GET_BUS_NUMBER:
  492. ret = scsi_ioctl(sdev, cmd, argp);
  493. goto out;
  494. }
  495. ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
  496. if (ret != -ENOSYS)
  497. goto out;
  498. ret = scsi_ioctl(sdev, cmd, argp);
  499. out:
  500. mutex_unlock(&sr_mutex);
  501. return ret;
  502. }
  503. static unsigned int sr_block_check_events(struct gendisk *disk,
  504. unsigned int clearing)
  505. {
  506. unsigned int ret = 0;
  507. struct scsi_cd *cd;
  508. cd = scsi_cd_get(disk);
  509. if (!cd)
  510. return 0;
  511. if (!atomic_read(&cd->device->disk_events_disable_depth))
  512. ret = cdrom_check_events(&cd->cdi, clearing);
  513. scsi_cd_put(cd);
  514. return ret;
  515. }
  516. static int sr_block_revalidate_disk(struct gendisk *disk)
  517. {
  518. struct scsi_sense_hdr sshdr;
  519. struct scsi_cd *cd;
  520. cd = scsi_cd_get(disk);
  521. if (!cd)
  522. return -ENXIO;
  523. /* if the unit is not ready, nothing more to do */
  524. if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
  525. goto out;
  526. sr_cd_check(&cd->cdi);
  527. get_sectorsize(cd);
  528. out:
  529. scsi_cd_put(cd);
  530. return 0;
  531. }
  532. static const struct block_device_operations sr_bdops =
  533. {
  534. .owner = THIS_MODULE,
  535. .open = sr_block_open,
  536. .release = sr_block_release,
  537. .ioctl = sr_block_ioctl,
  538. .check_events = sr_block_check_events,
  539. .revalidate_disk = sr_block_revalidate_disk,
  540. /*
  541. * No compat_ioctl for now because sr_block_ioctl never
  542. * seems to pass arbitrary ioctls down to host drivers.
  543. */
  544. };
  545. static int sr_open(struct cdrom_device_info *cdi, int purpose)
  546. {
  547. struct scsi_cd *cd = cdi->handle;
  548. struct scsi_device *sdev = cd->device;
  549. int retval;
  550. /*
  551. * If the device is in error recovery, wait until it is done.
  552. * If the device is offline, then disallow any access to it.
  553. */
  554. retval = -ENXIO;
  555. if (!scsi_block_when_processing_errors(sdev))
  556. goto error_out;
  557. return 0;
  558. error_out:
  559. return retval;
  560. }
  561. static void sr_release(struct cdrom_device_info *cdi)
  562. {
  563. struct scsi_cd *cd = cdi->handle;
  564. if (cd->device->sector_size > 2048)
  565. sr_set_blocklength(cd, 2048);
  566. }
  567. static int sr_probe(struct device *dev)
  568. {
  569. struct scsi_device *sdev = to_scsi_device(dev);
  570. struct gendisk *disk;
  571. struct scsi_cd *cd;
  572. int minor, error;
  573. scsi_autopm_get_device(sdev);
  574. error = -ENODEV;
  575. if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
  576. goto fail;
  577. error = -ENOMEM;
  578. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  579. if (!cd)
  580. goto fail;
  581. kref_init(&cd->kref);
  582. disk = alloc_disk(1);
  583. if (!disk)
  584. goto fail_free;
  585. spin_lock(&sr_index_lock);
  586. minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
  587. if (minor == SR_DISKS) {
  588. spin_unlock(&sr_index_lock);
  589. error = -EBUSY;
  590. goto fail_put;
  591. }
  592. __set_bit(minor, sr_index_bits);
  593. spin_unlock(&sr_index_lock);
  594. disk->major = SCSI_CDROM_MAJOR;
  595. disk->first_minor = minor;
  596. sprintf(disk->disk_name, "sr%d", minor);
  597. disk->fops = &sr_bdops;
  598. disk->flags = GENHD_FL_CD | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
  599. disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
  600. blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
  601. cd->device = sdev;
  602. cd->disk = disk;
  603. cd->driver = &sr_template;
  604. cd->disk = disk;
  605. cd->capacity = 0x1fffff;
  606. cd->device->changed = 1; /* force recheck CD type */
  607. cd->media_present = 1;
  608. cd->use = 1;
  609. cd->readcd_known = 0;
  610. cd->readcd_cdda = 0;
  611. cd->cdi.ops = &sr_dops;
  612. cd->cdi.handle = cd;
  613. cd->cdi.mask = 0;
  614. cd->cdi.capacity = 1;
  615. sprintf(cd->cdi.name, "sr%d", minor);
  616. sdev->sector_size = 2048; /* A guess, just in case */
  617. /* FIXME: need to handle a get_capabilities failure properly ?? */
  618. get_capabilities(cd);
  619. sr_vendor_init(cd);
  620. set_capacity(disk, cd->capacity);
  621. disk->private_data = &cd->driver;
  622. disk->queue = sdev->request_queue;
  623. cd->cdi.disk = disk;
  624. if (register_cdrom(&cd->cdi))
  625. goto fail_put;
  626. /*
  627. * Initialize block layer runtime PM stuffs before the
  628. * periodic event checking request gets started in add_disk.
  629. */
  630. blk_pm_runtime_init(sdev->request_queue, dev);
  631. dev_set_drvdata(dev, cd);
  632. disk->flags |= GENHD_FL_REMOVABLE;
  633. device_add_disk(&sdev->sdev_gendev, disk);
  634. sdev_printk(KERN_DEBUG, sdev,
  635. "Attached scsi CD-ROM %s\n", cd->cdi.name);
  636. scsi_autopm_put_device(cd->device);
  637. return 0;
  638. fail_put:
  639. put_disk(disk);
  640. fail_free:
  641. kfree(cd);
  642. fail:
  643. scsi_autopm_put_device(sdev);
  644. return error;
  645. }
  646. static void get_sectorsize(struct scsi_cd *cd)
  647. {
  648. unsigned char cmd[10];
  649. unsigned char buffer[8];
  650. int the_result, retries = 3;
  651. int sector_size;
  652. struct request_queue *queue;
  653. do {
  654. cmd[0] = READ_CAPACITY;
  655. memset((void *) &cmd[1], 0, 9);
  656. memset(buffer, 0, sizeof(buffer));
  657. /* Do the command and wait.. */
  658. the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
  659. buffer, sizeof(buffer), NULL,
  660. SR_TIMEOUT, MAX_RETRIES, NULL);
  661. retries--;
  662. } while (the_result && retries);
  663. if (the_result) {
  664. cd->capacity = 0x1fffff;
  665. sector_size = 2048; /* A guess, just in case */
  666. } else {
  667. long last_written;
  668. cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
  669. (buffer[2] << 8) | buffer[3]);
  670. /*
  671. * READ_CAPACITY doesn't return the correct size on
  672. * certain UDF media. If last_written is larger, use
  673. * it instead.
  674. *
  675. * http://bugzilla.kernel.org/show_bug.cgi?id=9668
  676. */
  677. if (!cdrom_get_last_written(&cd->cdi, &last_written))
  678. cd->capacity = max_t(long, cd->capacity, last_written);
  679. sector_size = (buffer[4] << 24) |
  680. (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  681. switch (sector_size) {
  682. /*
  683. * HP 4020i CD-Recorder reports 2340 byte sectors
  684. * Philips CD-Writers report 2352 byte sectors
  685. *
  686. * Use 2k sectors for them..
  687. */
  688. case 0:
  689. case 2340:
  690. case 2352:
  691. sector_size = 2048;
  692. /* fall through */
  693. case 2048:
  694. cd->capacity *= 4;
  695. /* fall through */
  696. case 512:
  697. break;
  698. default:
  699. sr_printk(KERN_INFO, cd,
  700. "unsupported sector size %d.", sector_size);
  701. cd->capacity = 0;
  702. }
  703. cd->device->sector_size = sector_size;
  704. /*
  705. * Add this so that we have the ability to correctly gauge
  706. * what the device is capable of.
  707. */
  708. set_capacity(cd->disk, cd->capacity);
  709. }
  710. queue = cd->device->request_queue;
  711. blk_queue_logical_block_size(queue, sector_size);
  712. return;
  713. }
  714. static void get_capabilities(struct scsi_cd *cd)
  715. {
  716. unsigned char *buffer;
  717. struct scsi_mode_data data;
  718. struct scsi_sense_hdr sshdr;
  719. unsigned int ms_len = 128;
  720. int rc, n;
  721. static const char *loadmech[] =
  722. {
  723. "caddy",
  724. "tray",
  725. "pop-up",
  726. "",
  727. "changer",
  728. "cartridge changer",
  729. "",
  730. ""
  731. };
  732. /* allocate transfer buffer */
  733. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  734. if (!buffer) {
  735. sr_printk(KERN_ERR, cd, "out of memory.\n");
  736. return;
  737. }
  738. /* eat unit attentions */
  739. scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
  740. /* ask for mode page 0x2a */
  741. rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len,
  742. SR_TIMEOUT, 3, &data, NULL);
  743. if (!scsi_status_is_good(rc) || data.length > ms_len ||
  744. data.header_length + data.block_descriptor_length > data.length) {
  745. /* failed, drive doesn't have capabilities mode page */
  746. cd->cdi.speed = 1;
  747. cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
  748. CDC_DVD | CDC_DVD_RAM |
  749. CDC_SELECT_DISC | CDC_SELECT_SPEED |
  750. CDC_MRW | CDC_MRW_W | CDC_RAM);
  751. kfree(buffer);
  752. sr_printk(KERN_INFO, cd, "scsi-1 drive");
  753. return;
  754. }
  755. n = data.header_length + data.block_descriptor_length;
  756. cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
  757. cd->readcd_known = 1;
  758. cd->readcd_cdda = buffer[n + 5] & 0x01;
  759. /* print some capability bits */
  760. sr_printk(KERN_INFO, cd,
  761. "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n",
  762. ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
  763. cd->cdi.speed,
  764. buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
  765. buffer[n + 3] & 0x20 ? "dvd-ram " : "",
  766. buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
  767. buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
  768. buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
  769. loadmech[buffer[n + 6] >> 5]);
  770. if ((buffer[n + 6] >> 5) == 0)
  771. /* caddy drives can't close tray... */
  772. cd->cdi.mask |= CDC_CLOSE_TRAY;
  773. if ((buffer[n + 2] & 0x8) == 0)
  774. /* not a DVD drive */
  775. cd->cdi.mask |= CDC_DVD;
  776. if ((buffer[n + 3] & 0x20) == 0)
  777. /* can't write DVD-RAM media */
  778. cd->cdi.mask |= CDC_DVD_RAM;
  779. if ((buffer[n + 3] & 0x10) == 0)
  780. /* can't write DVD-R media */
  781. cd->cdi.mask |= CDC_DVD_R;
  782. if ((buffer[n + 3] & 0x2) == 0)
  783. /* can't write CD-RW media */
  784. cd->cdi.mask |= CDC_CD_RW;
  785. if ((buffer[n + 3] & 0x1) == 0)
  786. /* can't write CD-R media */
  787. cd->cdi.mask |= CDC_CD_R;
  788. if ((buffer[n + 6] & 0x8) == 0)
  789. /* can't eject */
  790. cd->cdi.mask |= CDC_OPEN_TRAY;
  791. if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
  792. (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
  793. cd->cdi.capacity =
  794. cdrom_number_of_slots(&cd->cdi);
  795. if (cd->cdi.capacity <= 1)
  796. /* not a changer */
  797. cd->cdi.mask |= CDC_SELECT_DISC;
  798. /*else I don't think it can close its tray
  799. cd->cdi.mask |= CDC_CLOSE_TRAY; */
  800. /*
  801. * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
  802. */
  803. if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
  804. (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
  805. cd->writeable = 1;
  806. }
  807. kfree(buffer);
  808. }
  809. /*
  810. * sr_packet() is the entry point for the generic commands generated
  811. * by the Uniform CD-ROM layer.
  812. */
  813. static int sr_packet(struct cdrom_device_info *cdi,
  814. struct packet_command *cgc)
  815. {
  816. struct scsi_cd *cd = cdi->handle;
  817. struct scsi_device *sdev = cd->device;
  818. if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
  819. return -EDRIVE_CANT_DO_THIS;
  820. if (cgc->timeout <= 0)
  821. cgc->timeout = IOCTL_TIMEOUT;
  822. sr_do_ioctl(cd, cgc);
  823. return cgc->stat;
  824. }
  825. /**
  826. * sr_kref_release - Called to free the scsi_cd structure
  827. * @kref: pointer to embedded kref
  828. *
  829. * sr_ref_mutex must be held entering this routine. Because it is
  830. * called on last put, you should always use the scsi_cd_get()
  831. * scsi_cd_put() helpers which manipulate the semaphore directly
  832. * and never do a direct kref_put().
  833. **/
  834. static void sr_kref_release(struct kref *kref)
  835. {
  836. struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
  837. struct gendisk *disk = cd->disk;
  838. spin_lock(&sr_index_lock);
  839. clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
  840. spin_unlock(&sr_index_lock);
  841. unregister_cdrom(&cd->cdi);
  842. disk->private_data = NULL;
  843. put_disk(disk);
  844. kfree(cd);
  845. }
  846. static int sr_remove(struct device *dev)
  847. {
  848. struct scsi_cd *cd = dev_get_drvdata(dev);
  849. scsi_autopm_get_device(cd->device);
  850. del_gendisk(cd->disk);
  851. dev_set_drvdata(dev, NULL);
  852. mutex_lock(&sr_ref_mutex);
  853. kref_put(&cd->kref, sr_kref_release);
  854. mutex_unlock(&sr_ref_mutex);
  855. return 0;
  856. }
  857. static int __init init_sr(void)
  858. {
  859. int rc;
  860. rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
  861. if (rc)
  862. return rc;
  863. rc = scsi_register_driver(&sr_template.gendrv);
  864. if (rc)
  865. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  866. return rc;
  867. }
  868. static void __exit exit_sr(void)
  869. {
  870. scsi_unregister_driver(&sr_template.gendrv);
  871. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  872. }
  873. module_init(init_sr);
  874. module_exit(exit_sr);
  875. MODULE_LICENSE("GPL");