sr.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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_done(struct scsi_cmnd *);
  75. static int sr_runtime_suspend(struct device *dev);
  76. static struct dev_pm_ops sr_pm_ops = {
  77. .runtime_suspend = sr_runtime_suspend,
  78. };
  79. static struct scsi_driver sr_template = {
  80. .owner = THIS_MODULE,
  81. .gendrv = {
  82. .name = "sr",
  83. .probe = sr_probe,
  84. .remove = sr_remove,
  85. .pm = &sr_pm_ops,
  86. },
  87. .done = sr_done,
  88. };
  89. static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
  90. static DEFINE_SPINLOCK(sr_index_lock);
  91. /* This semaphore is used to mediate the 0->1 reference get in the
  92. * face of object destruction (i.e. we can't allow a get on an
  93. * object after last put) */
  94. static DEFINE_MUTEX(sr_ref_mutex);
  95. static int sr_open(struct cdrom_device_info *, int);
  96. static void sr_release(struct cdrom_device_info *);
  97. static void get_sectorsize(struct scsi_cd *);
  98. static void get_capabilities(struct scsi_cd *);
  99. static unsigned int sr_check_events(struct cdrom_device_info *cdi,
  100. unsigned int clearing, int slot);
  101. static int sr_packet(struct cdrom_device_info *, struct packet_command *);
  102. static struct cdrom_device_ops sr_dops = {
  103. .open = sr_open,
  104. .release = sr_release,
  105. .drive_status = sr_drive_status,
  106. .check_events = sr_check_events,
  107. .tray_move = sr_tray_move,
  108. .lock_door = sr_lock_door,
  109. .select_speed = sr_select_speed,
  110. .get_last_session = sr_get_last_session,
  111. .get_mcn = sr_get_mcn,
  112. .reset = sr_reset,
  113. .audio_ioctl = sr_audio_ioctl,
  114. .capability = SR_CAPABILITIES,
  115. .generic_packet = sr_packet,
  116. };
  117. static void sr_kref_release(struct kref *kref);
  118. static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
  119. {
  120. return container_of(disk->private_data, struct scsi_cd, driver);
  121. }
  122. static int sr_runtime_suspend(struct device *dev)
  123. {
  124. struct scsi_cd *cd = dev_get_drvdata(dev);
  125. if (cd->media_present)
  126. return -EBUSY;
  127. else
  128. return 0;
  129. }
  130. /*
  131. * The get and put routines for the struct scsi_cd. Note this entity
  132. * has a scsi_device pointer and owns a reference to this.
  133. */
  134. static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
  135. {
  136. struct scsi_cd *cd = NULL;
  137. mutex_lock(&sr_ref_mutex);
  138. if (disk->private_data == NULL)
  139. goto out;
  140. cd = scsi_cd(disk);
  141. kref_get(&cd->kref);
  142. if (scsi_device_get(cd->device))
  143. goto out_put;
  144. if (!scsi_autopm_get_device(cd->device))
  145. goto out;
  146. out_put:
  147. kref_put(&cd->kref, sr_kref_release);
  148. cd = NULL;
  149. out:
  150. mutex_unlock(&sr_ref_mutex);
  151. return cd;
  152. }
  153. static void scsi_cd_put(struct scsi_cd *cd)
  154. {
  155. struct scsi_device *sdev = cd->device;
  156. mutex_lock(&sr_ref_mutex);
  157. kref_put(&cd->kref, sr_kref_release);
  158. scsi_autopm_put_device(sdev);
  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. sdev_printk(KERN_WARNING, cd->device,
  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. printk("sr.c 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_prep_fn(struct request_queue *q, struct request *rq)
  338. {
  339. int block = 0, this_count, s_size;
  340. struct scsi_cd *cd;
  341. struct scsi_cmnd *SCpnt;
  342. struct scsi_device *sdp = q->queuedata;
  343. int ret;
  344. if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  345. ret = scsi_setup_blk_pc_cmnd(sdp, rq);
  346. goto out;
  347. } else if (rq->cmd_type != REQ_TYPE_FS) {
  348. ret = BLKPREP_KILL;
  349. goto out;
  350. }
  351. ret = scsi_setup_fs_cmnd(sdp, rq);
  352. if (ret != BLKPREP_OK)
  353. goto out;
  354. SCpnt = rq->special;
  355. cd = scsi_cd(rq->rq_disk);
  356. /* from here on until we're complete, any goto out
  357. * is used for a killable error condition */
  358. ret = BLKPREP_KILL;
  359. SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
  360. cd->disk->disk_name, block));
  361. if (!cd->device || !scsi_device_online(cd->device)) {
  362. SCSI_LOG_HLQUEUE(2, printk("Finishing %u sectors\n",
  363. blk_rq_sectors(rq)));
  364. SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
  365. goto out;
  366. }
  367. if (cd->device->changed) {
  368. /*
  369. * quietly refuse to do anything to a changed disc until the
  370. * changed bit has been reset
  371. */
  372. goto out;
  373. }
  374. /*
  375. * we do lazy blocksize switching (when reading XA sectors,
  376. * see CDROMREADMODE2 ioctl)
  377. */
  378. s_size = cd->device->sector_size;
  379. if (s_size > 2048) {
  380. if (!in_interrupt())
  381. sr_set_blocklength(cd, 2048);
  382. else
  383. printk("sr: can't switch blocksize: in interrupt\n");
  384. }
  385. if (s_size != 512 && s_size != 1024 && s_size != 2048) {
  386. scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
  387. goto out;
  388. }
  389. if (rq_data_dir(rq) == WRITE) {
  390. if (!cd->device->writeable)
  391. goto out;
  392. SCpnt->cmnd[0] = WRITE_10;
  393. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  394. cd->cdi.media_written = 1;
  395. } else if (rq_data_dir(rq) == READ) {
  396. SCpnt->cmnd[0] = READ_10;
  397. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  398. } else {
  399. blk_dump_rq_flags(rq, "Unknown sr command");
  400. goto out;
  401. }
  402. {
  403. struct scatterlist *sg;
  404. int i, size = 0, sg_count = scsi_sg_count(SCpnt);
  405. scsi_for_each_sg(SCpnt, sg, sg_count, i)
  406. size += sg->length;
  407. if (size != scsi_bufflen(SCpnt)) {
  408. scmd_printk(KERN_ERR, SCpnt,
  409. "mismatch count %d, bytes %d\n",
  410. size, scsi_bufflen(SCpnt));
  411. if (scsi_bufflen(SCpnt) > size)
  412. SCpnt->sdb.length = size;
  413. }
  414. }
  415. /*
  416. * request doesn't start on hw block boundary, add scatter pads
  417. */
  418. if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
  419. (scsi_bufflen(SCpnt) % s_size)) {
  420. scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
  421. goto out;
  422. }
  423. this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
  424. SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%u 512 byte blocks.\n",
  425. cd->cdi.name,
  426. (rq_data_dir(rq) == WRITE) ?
  427. "writing" : "reading",
  428. this_count, blk_rq_sectors(rq)));
  429. SCpnt->cmnd[1] = 0;
  430. block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
  431. if (this_count > 0xffff) {
  432. this_count = 0xffff;
  433. SCpnt->sdb.length = this_count * s_size;
  434. }
  435. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  436. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  437. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  438. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  439. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  440. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  441. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  442. /*
  443. * We shouldn't disconnect in the middle of a sector, so with a dumb
  444. * host adapter, it's safe to assume that we can at least transfer
  445. * this many bytes between each connect / disconnect.
  446. */
  447. SCpnt->transfersize = cd->device->sector_size;
  448. SCpnt->underflow = this_count << 9;
  449. SCpnt->allowed = MAX_RETRIES;
  450. /*
  451. * This indicates that the command is ready from our end to be
  452. * queued.
  453. */
  454. ret = BLKPREP_OK;
  455. out:
  456. return scsi_prep_return(q, rq, ret);
  457. }
  458. static int sr_block_open(struct block_device *bdev, fmode_t mode)
  459. {
  460. struct scsi_cd *cd;
  461. int ret = -ENXIO;
  462. mutex_lock(&sr_mutex);
  463. cd = scsi_cd_get(bdev->bd_disk);
  464. if (cd) {
  465. ret = cdrom_open(&cd->cdi, bdev, mode);
  466. if (ret)
  467. scsi_cd_put(cd);
  468. }
  469. mutex_unlock(&sr_mutex);
  470. return ret;
  471. }
  472. static int sr_block_release(struct gendisk *disk, fmode_t mode)
  473. {
  474. struct scsi_cd *cd = scsi_cd(disk);
  475. mutex_lock(&sr_mutex);
  476. cdrom_release(&cd->cdi, mode);
  477. scsi_cd_put(cd);
  478. mutex_unlock(&sr_mutex);
  479. return 0;
  480. }
  481. static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
  482. unsigned long arg)
  483. {
  484. struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
  485. struct scsi_device *sdev = cd->device;
  486. void __user *argp = (void __user *)arg;
  487. int ret;
  488. scsi_autopm_get_device(cd->device);
  489. mutex_lock(&sr_mutex);
  490. /*
  491. * Send SCSI addressing ioctls directly to mid level, send other
  492. * ioctls to cdrom/block level.
  493. */
  494. switch (cmd) {
  495. case SCSI_IOCTL_GET_IDLUN:
  496. case SCSI_IOCTL_GET_BUS_NUMBER:
  497. ret = scsi_ioctl(sdev, cmd, argp);
  498. goto out;
  499. }
  500. ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
  501. if (ret != -ENOSYS)
  502. goto out;
  503. /*
  504. * ENODEV means that we didn't recognise the ioctl, or that we
  505. * cannot execute it in the current device state. In either
  506. * case fall through to scsi_ioctl, which will return ENDOEV again
  507. * if it doesn't recognise the ioctl
  508. */
  509. ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
  510. (mode & FMODE_NDELAY) != 0);
  511. if (ret != -ENODEV)
  512. goto out;
  513. ret = scsi_ioctl(sdev, cmd, argp);
  514. out:
  515. mutex_unlock(&sr_mutex);
  516. scsi_autopm_put_device(cd->device);
  517. return ret;
  518. }
  519. static unsigned int sr_block_check_events(struct gendisk *disk,
  520. unsigned int clearing)
  521. {
  522. struct scsi_cd *cd = scsi_cd(disk);
  523. unsigned int ret;
  524. if (atomic_read(&cd->device->disk_events_disable_depth) == 0) {
  525. scsi_autopm_get_device(cd->device);
  526. ret = cdrom_check_events(&cd->cdi, clearing);
  527. scsi_autopm_put_device(cd->device);
  528. } else {
  529. ret = 0;
  530. }
  531. return ret;
  532. }
  533. static int sr_block_revalidate_disk(struct gendisk *disk)
  534. {
  535. struct scsi_cd *cd = scsi_cd(disk);
  536. struct scsi_sense_hdr sshdr;
  537. scsi_autopm_get_device(cd->device);
  538. /* if the unit is not ready, nothing more to do */
  539. if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
  540. goto out;
  541. sr_cd_check(&cd->cdi);
  542. get_sectorsize(cd);
  543. out:
  544. scsi_autopm_put_device(cd->device);
  545. return 0;
  546. }
  547. static const struct block_device_operations sr_bdops =
  548. {
  549. .owner = THIS_MODULE,
  550. .open = sr_block_open,
  551. .release = sr_block_release,
  552. .ioctl = sr_block_ioctl,
  553. .check_events = sr_block_check_events,
  554. .revalidate_disk = sr_block_revalidate_disk,
  555. /*
  556. * No compat_ioctl for now because sr_block_ioctl never
  557. * seems to pass arbitrary ioctls down to host drivers.
  558. */
  559. };
  560. static int sr_open(struct cdrom_device_info *cdi, int purpose)
  561. {
  562. struct scsi_cd *cd = cdi->handle;
  563. struct scsi_device *sdev = cd->device;
  564. int retval;
  565. /*
  566. * If the device is in error recovery, wait until it is done.
  567. * If the device is offline, then disallow any access to it.
  568. */
  569. retval = -ENXIO;
  570. if (!scsi_block_when_processing_errors(sdev))
  571. goto error_out;
  572. return 0;
  573. error_out:
  574. return retval;
  575. }
  576. static void sr_release(struct cdrom_device_info *cdi)
  577. {
  578. struct scsi_cd *cd = cdi->handle;
  579. if (cd->device->sector_size > 2048)
  580. sr_set_blocklength(cd, 2048);
  581. }
  582. static int sr_probe(struct device *dev)
  583. {
  584. struct scsi_device *sdev = to_scsi_device(dev);
  585. struct gendisk *disk;
  586. struct scsi_cd *cd;
  587. int minor, error;
  588. error = -ENODEV;
  589. if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
  590. goto fail;
  591. error = -ENOMEM;
  592. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  593. if (!cd)
  594. goto fail;
  595. kref_init(&cd->kref);
  596. disk = alloc_disk(1);
  597. if (!disk)
  598. goto fail_free;
  599. spin_lock(&sr_index_lock);
  600. minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
  601. if (minor == SR_DISKS) {
  602. spin_unlock(&sr_index_lock);
  603. error = -EBUSY;
  604. goto fail_put;
  605. }
  606. __set_bit(minor, sr_index_bits);
  607. spin_unlock(&sr_index_lock);
  608. disk->major = SCSI_CDROM_MAJOR;
  609. disk->first_minor = minor;
  610. sprintf(disk->disk_name, "sr%d", minor);
  611. disk->fops = &sr_bdops;
  612. disk->flags = GENHD_FL_CD | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
  613. disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
  614. blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
  615. cd->device = sdev;
  616. cd->disk = disk;
  617. cd->driver = &sr_template;
  618. cd->disk = disk;
  619. cd->capacity = 0x1fffff;
  620. cd->device->changed = 1; /* force recheck CD type */
  621. cd->media_present = 1;
  622. cd->use = 1;
  623. cd->readcd_known = 0;
  624. cd->readcd_cdda = 0;
  625. cd->cdi.ops = &sr_dops;
  626. cd->cdi.handle = cd;
  627. cd->cdi.mask = 0;
  628. cd->cdi.capacity = 1;
  629. sprintf(cd->cdi.name, "sr%d", minor);
  630. sdev->sector_size = 2048; /* A guess, just in case */
  631. /* FIXME: need to handle a get_capabilities failure properly ?? */
  632. get_capabilities(cd);
  633. blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
  634. sr_vendor_init(cd);
  635. disk->driverfs_dev = &sdev->sdev_gendev;
  636. set_capacity(disk, cd->capacity);
  637. disk->private_data = &cd->driver;
  638. disk->queue = sdev->request_queue;
  639. cd->cdi.disk = disk;
  640. if (register_cdrom(&cd->cdi))
  641. goto fail_put;
  642. dev_set_drvdata(dev, cd);
  643. disk->flags |= GENHD_FL_REMOVABLE;
  644. add_disk(disk);
  645. sdev_printk(KERN_DEBUG, sdev,
  646. "Attached scsi CD-ROM %s\n", cd->cdi.name);
  647. scsi_autopm_put_device(cd->device);
  648. return 0;
  649. fail_put:
  650. put_disk(disk);
  651. fail_free:
  652. kfree(cd);
  653. fail:
  654. return error;
  655. }
  656. static void get_sectorsize(struct scsi_cd *cd)
  657. {
  658. unsigned char cmd[10];
  659. unsigned char buffer[8];
  660. int the_result, retries = 3;
  661. int sector_size;
  662. struct request_queue *queue;
  663. do {
  664. cmd[0] = READ_CAPACITY;
  665. memset((void *) &cmd[1], 0, 9);
  666. memset(buffer, 0, sizeof(buffer));
  667. /* Do the command and wait.. */
  668. the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
  669. buffer, sizeof(buffer), NULL,
  670. SR_TIMEOUT, MAX_RETRIES, NULL);
  671. retries--;
  672. } while (the_result && retries);
  673. if (the_result) {
  674. cd->capacity = 0x1fffff;
  675. sector_size = 2048; /* A guess, just in case */
  676. } else {
  677. long last_written;
  678. cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
  679. (buffer[2] << 8) | buffer[3]);
  680. /*
  681. * READ_CAPACITY doesn't return the correct size on
  682. * certain UDF media. If last_written is larger, use
  683. * it instead.
  684. *
  685. * http://bugzilla.kernel.org/show_bug.cgi?id=9668
  686. */
  687. if (!cdrom_get_last_written(&cd->cdi, &last_written))
  688. cd->capacity = max_t(long, cd->capacity, last_written);
  689. sector_size = (buffer[4] << 24) |
  690. (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  691. switch (sector_size) {
  692. /*
  693. * HP 4020i CD-Recorder reports 2340 byte sectors
  694. * Philips CD-Writers report 2352 byte sectors
  695. *
  696. * Use 2k sectors for them..
  697. */
  698. case 0:
  699. case 2340:
  700. case 2352:
  701. sector_size = 2048;
  702. /* fall through */
  703. case 2048:
  704. cd->capacity *= 4;
  705. /* fall through */
  706. case 512:
  707. break;
  708. default:
  709. printk("%s: unsupported sector size %d.\n",
  710. cd->cdi.name, sector_size);
  711. cd->capacity = 0;
  712. }
  713. cd->device->sector_size = sector_size;
  714. /*
  715. * Add this so that we have the ability to correctly gauge
  716. * what the device is capable of.
  717. */
  718. set_capacity(cd->disk, cd->capacity);
  719. }
  720. queue = cd->device->request_queue;
  721. blk_queue_logical_block_size(queue, sector_size);
  722. return;
  723. }
  724. static void get_capabilities(struct scsi_cd *cd)
  725. {
  726. unsigned char *buffer;
  727. struct scsi_mode_data data;
  728. struct scsi_sense_hdr sshdr;
  729. int rc, n;
  730. static const char *loadmech[] =
  731. {
  732. "caddy",
  733. "tray",
  734. "pop-up",
  735. "",
  736. "changer",
  737. "cartridge changer",
  738. "",
  739. ""
  740. };
  741. /* allocate transfer buffer */
  742. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  743. if (!buffer) {
  744. printk(KERN_ERR "sr: out of memory.\n");
  745. return;
  746. }
  747. /* eat unit attentions */
  748. scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
  749. /* ask for mode page 0x2a */
  750. rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
  751. SR_TIMEOUT, 3, &data, NULL);
  752. if (!scsi_status_is_good(rc)) {
  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. printk("%s: scsi-1 drive\n", cd->cdi.name);
  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. printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
  769. ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
  770. cd->cdi.speed,
  771. buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
  772. buffer[n + 3] & 0x20 ? "dvd-ram " : "",
  773. buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
  774. buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
  775. buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
  776. loadmech[buffer[n + 6] >> 5]);
  777. if ((buffer[n + 6] >> 5) == 0)
  778. /* caddy drives can't close tray... */
  779. cd->cdi.mask |= CDC_CLOSE_TRAY;
  780. if ((buffer[n + 2] & 0x8) == 0)
  781. /* not a DVD drive */
  782. cd->cdi.mask |= CDC_DVD;
  783. if ((buffer[n + 3] & 0x20) == 0)
  784. /* can't write DVD-RAM media */
  785. cd->cdi.mask |= CDC_DVD_RAM;
  786. if ((buffer[n + 3] & 0x10) == 0)
  787. /* can't write DVD-R media */
  788. cd->cdi.mask |= CDC_DVD_R;
  789. if ((buffer[n + 3] & 0x2) == 0)
  790. /* can't write CD-RW media */
  791. cd->cdi.mask |= CDC_CD_RW;
  792. if ((buffer[n + 3] & 0x1) == 0)
  793. /* can't write CD-R media */
  794. cd->cdi.mask |= CDC_CD_R;
  795. if ((buffer[n + 6] & 0x8) == 0)
  796. /* can't eject */
  797. cd->cdi.mask |= CDC_OPEN_TRAY;
  798. if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
  799. (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
  800. cd->cdi.capacity =
  801. cdrom_number_of_slots(&cd->cdi);
  802. if (cd->cdi.capacity <= 1)
  803. /* not a changer */
  804. cd->cdi.mask |= CDC_SELECT_DISC;
  805. /*else I don't think it can close its tray
  806. cd->cdi.mask |= CDC_CLOSE_TRAY; */
  807. /*
  808. * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
  809. */
  810. if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
  811. (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
  812. cd->device->writeable = 1;
  813. }
  814. kfree(buffer);
  815. }
  816. /*
  817. * sr_packet() is the entry point for the generic commands generated
  818. * by the Uniform CD-ROM layer.
  819. */
  820. static int sr_packet(struct cdrom_device_info *cdi,
  821. struct packet_command *cgc)
  822. {
  823. struct scsi_cd *cd = cdi->handle;
  824. struct scsi_device *sdev = cd->device;
  825. if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
  826. return -EDRIVE_CANT_DO_THIS;
  827. if (cgc->timeout <= 0)
  828. cgc->timeout = IOCTL_TIMEOUT;
  829. sr_do_ioctl(cd, cgc);
  830. return cgc->stat;
  831. }
  832. /**
  833. * sr_kref_release - Called to free the scsi_cd structure
  834. * @kref: pointer to embedded kref
  835. *
  836. * sr_ref_mutex must be held entering this routine. Because it is
  837. * called on last put, you should always use the scsi_cd_get()
  838. * scsi_cd_put() helpers which manipulate the semaphore directly
  839. * and never do a direct kref_put().
  840. **/
  841. static void sr_kref_release(struct kref *kref)
  842. {
  843. struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
  844. struct gendisk *disk = cd->disk;
  845. spin_lock(&sr_index_lock);
  846. clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
  847. spin_unlock(&sr_index_lock);
  848. unregister_cdrom(&cd->cdi);
  849. disk->private_data = NULL;
  850. put_disk(disk);
  851. kfree(cd);
  852. }
  853. static int sr_remove(struct device *dev)
  854. {
  855. struct scsi_cd *cd = dev_get_drvdata(dev);
  856. scsi_autopm_get_device(cd->device);
  857. blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
  858. del_gendisk(cd->disk);
  859. mutex_lock(&sr_ref_mutex);
  860. kref_put(&cd->kref, sr_kref_release);
  861. mutex_unlock(&sr_ref_mutex);
  862. return 0;
  863. }
  864. static int __init init_sr(void)
  865. {
  866. int rc;
  867. rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
  868. if (rc)
  869. return rc;
  870. rc = scsi_register_driver(&sr_template.gendrv);
  871. if (rc)
  872. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  873. return rc;
  874. }
  875. static void __exit exit_sr(void)
  876. {
  877. scsi_unregister_driver(&sr_template.gendrv);
  878. unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
  879. }
  880. module_init(init_sr);
  881. module_exit(exit_sr);
  882. MODULE_LICENSE("GPL");