sr.c 24 KB

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