sr.c 25 KB

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