scsi_ioctl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public Licens
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/string.h>
  22. #include <linux/module.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/capability.h>
  25. #include <linux/completion.h>
  26. #include <linux/cdrom.h>
  27. #include <linux/slab.h>
  28. #include <linux/times.h>
  29. #include <asm/uaccess.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_ioctl.h>
  32. #include <scsi/scsi_cmnd.h>
  33. /* Command group 3 is reserved and should never be used. */
  34. const unsigned char scsi_command_size[8] =
  35. {
  36. 6, 10, 10, 12,
  37. 16, 12, 10, 10
  38. };
  39. EXPORT_SYMBOL(scsi_command_size);
  40. #define BLK_DEFAULT_TIMEOUT (60 * HZ)
  41. #include <scsi/sg.h>
  42. static int sg_get_version(int __user *p)
  43. {
  44. static const int sg_version_num = 30527;
  45. return put_user(sg_version_num, p);
  46. }
  47. static int scsi_get_idlun(request_queue_t *q, int __user *p)
  48. {
  49. return put_user(0, p);
  50. }
  51. static int scsi_get_bus(request_queue_t *q, int __user *p)
  52. {
  53. return put_user(0, p);
  54. }
  55. static int sg_get_timeout(request_queue_t *q)
  56. {
  57. return q->sg_timeout / (HZ / USER_HZ);
  58. }
  59. static int sg_set_timeout(request_queue_t *q, int __user *p)
  60. {
  61. int timeout, err = get_user(timeout, p);
  62. if (!err)
  63. q->sg_timeout = timeout * (HZ / USER_HZ);
  64. return err;
  65. }
  66. static int sg_get_reserved_size(request_queue_t *q, int __user *p)
  67. {
  68. return put_user(q->sg_reserved_size, p);
  69. }
  70. static int sg_set_reserved_size(request_queue_t *q, int __user *p)
  71. {
  72. int size, err = get_user(size, p);
  73. if (err)
  74. return err;
  75. if (size < 0)
  76. return -EINVAL;
  77. if (size > (q->max_sectors << 9))
  78. size = q->max_sectors << 9;
  79. q->sg_reserved_size = size;
  80. return 0;
  81. }
  82. /*
  83. * will always return that we are ATAPI even for a real SCSI drive, I'm not
  84. * so sure this is worth doing anything about (why would you care??)
  85. */
  86. static int sg_emulated_host(request_queue_t *q, int __user *p)
  87. {
  88. return put_user(1, p);
  89. }
  90. #define CMD_READ_SAFE 0x01
  91. #define CMD_WRITE_SAFE 0x02
  92. #define CMD_WARNED 0x04
  93. #define safe_for_read(cmd) [cmd] = CMD_READ_SAFE
  94. #define safe_for_write(cmd) [cmd] = CMD_WRITE_SAFE
  95. static int verify_command(struct file *file, unsigned char *cmd)
  96. {
  97. static unsigned char cmd_type[256] = {
  98. /* Basic read-only commands */
  99. safe_for_read(TEST_UNIT_READY),
  100. safe_for_read(REQUEST_SENSE),
  101. safe_for_read(READ_6),
  102. safe_for_read(READ_10),
  103. safe_for_read(READ_12),
  104. safe_for_read(READ_16),
  105. safe_for_read(READ_BUFFER),
  106. safe_for_read(READ_DEFECT_DATA),
  107. safe_for_read(READ_LONG),
  108. safe_for_read(INQUIRY),
  109. safe_for_read(MODE_SENSE),
  110. safe_for_read(MODE_SENSE_10),
  111. safe_for_read(LOG_SENSE),
  112. safe_for_read(START_STOP),
  113. safe_for_read(GPCMD_VERIFY_10),
  114. safe_for_read(VERIFY_16),
  115. /* Audio CD commands */
  116. safe_for_read(GPCMD_PLAY_CD),
  117. safe_for_read(GPCMD_PLAY_AUDIO_10),
  118. safe_for_read(GPCMD_PLAY_AUDIO_MSF),
  119. safe_for_read(GPCMD_PLAY_AUDIO_TI),
  120. safe_for_read(GPCMD_PAUSE_RESUME),
  121. /* CD/DVD data reading */
  122. safe_for_read(GPCMD_READ_BUFFER_CAPACITY),
  123. safe_for_read(GPCMD_READ_CD),
  124. safe_for_read(GPCMD_READ_CD_MSF),
  125. safe_for_read(GPCMD_READ_DISC_INFO),
  126. safe_for_read(GPCMD_READ_CDVD_CAPACITY),
  127. safe_for_read(GPCMD_READ_DVD_STRUCTURE),
  128. safe_for_read(GPCMD_READ_HEADER),
  129. safe_for_read(GPCMD_READ_TRACK_RZONE_INFO),
  130. safe_for_read(GPCMD_READ_SUBCHANNEL),
  131. safe_for_read(GPCMD_READ_TOC_PMA_ATIP),
  132. safe_for_read(GPCMD_REPORT_KEY),
  133. safe_for_read(GPCMD_SCAN),
  134. safe_for_read(GPCMD_GET_CONFIGURATION),
  135. safe_for_read(GPCMD_READ_FORMAT_CAPACITIES),
  136. safe_for_read(GPCMD_GET_EVENT_STATUS_NOTIFICATION),
  137. safe_for_read(GPCMD_GET_PERFORMANCE),
  138. safe_for_read(GPCMD_SEEK),
  139. safe_for_read(GPCMD_STOP_PLAY_SCAN),
  140. /* Basic writing commands */
  141. safe_for_write(WRITE_6),
  142. safe_for_write(WRITE_10),
  143. safe_for_write(WRITE_VERIFY),
  144. safe_for_write(WRITE_12),
  145. safe_for_write(WRITE_VERIFY_12),
  146. safe_for_write(WRITE_16),
  147. safe_for_write(WRITE_LONG),
  148. safe_for_write(WRITE_LONG_2),
  149. safe_for_write(ERASE),
  150. safe_for_write(GPCMD_MODE_SELECT_10),
  151. safe_for_write(MODE_SELECT),
  152. safe_for_write(LOG_SELECT),
  153. safe_for_write(GPCMD_BLANK),
  154. safe_for_write(GPCMD_CLOSE_TRACK),
  155. safe_for_write(GPCMD_FLUSH_CACHE),
  156. safe_for_write(GPCMD_FORMAT_UNIT),
  157. safe_for_write(GPCMD_REPAIR_RZONE_TRACK),
  158. safe_for_write(GPCMD_RESERVE_RZONE_TRACK),
  159. safe_for_write(GPCMD_SEND_DVD_STRUCTURE),
  160. safe_for_write(GPCMD_SEND_EVENT),
  161. safe_for_write(GPCMD_SEND_KEY),
  162. safe_for_write(GPCMD_SEND_OPC),
  163. safe_for_write(GPCMD_SEND_CUE_SHEET),
  164. safe_for_write(GPCMD_SET_SPEED),
  165. safe_for_write(GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL),
  166. safe_for_write(GPCMD_LOAD_UNLOAD),
  167. safe_for_write(GPCMD_SET_STREAMING),
  168. };
  169. unsigned char type = cmd_type[cmd[0]];
  170. int has_write_perm = 0;
  171. /* Anybody who can open the device can do a read-safe command */
  172. if (type & CMD_READ_SAFE)
  173. return 0;
  174. /*
  175. * file can be NULL from ioctl_by_bdev()...
  176. */
  177. if (file)
  178. has_write_perm = file->f_mode & FMODE_WRITE;
  179. /* Write-safe commands just require a writable open.. */
  180. if ((type & CMD_WRITE_SAFE) && has_write_perm)
  181. return 0;
  182. /* And root can do any command.. */
  183. if (capable(CAP_SYS_RAWIO))
  184. return 0;
  185. if (!type) {
  186. cmd_type[cmd[0]] = CMD_WARNED;
  187. printk(KERN_WARNING "scsi: unknown opcode 0x%02x\n", cmd[0]);
  188. }
  189. /* Otherwise fail it with an "Operation not permitted" */
  190. return -EPERM;
  191. }
  192. static int sg_io(struct file *file, request_queue_t *q,
  193. struct gendisk *bd_disk, struct sg_io_hdr *hdr)
  194. {
  195. unsigned long start_time;
  196. int writing = 0, ret = 0;
  197. struct request *rq;
  198. char sense[SCSI_SENSE_BUFFERSIZE];
  199. unsigned char cmd[BLK_MAX_CDB];
  200. if (hdr->interface_id != 'S')
  201. return -EINVAL;
  202. if (hdr->cmd_len > BLK_MAX_CDB)
  203. return -EINVAL;
  204. if (copy_from_user(cmd, hdr->cmdp, hdr->cmd_len))
  205. return -EFAULT;
  206. if (verify_command(file, cmd))
  207. return -EPERM;
  208. if (hdr->dxfer_len > (q->max_hw_sectors << 9))
  209. return -EIO;
  210. if (hdr->dxfer_len)
  211. switch (hdr->dxfer_direction) {
  212. default:
  213. return -EINVAL;
  214. case SG_DXFER_TO_DEV:
  215. writing = 1;
  216. break;
  217. case SG_DXFER_TO_FROM_DEV:
  218. case SG_DXFER_FROM_DEV:
  219. break;
  220. }
  221. rq = blk_get_request(q, writing ? WRITE : READ, GFP_KERNEL);
  222. if (!rq)
  223. return -ENOMEM;
  224. /*
  225. * fill in request structure
  226. */
  227. rq->cmd_len = hdr->cmd_len;
  228. memset(rq->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
  229. memcpy(rq->cmd, cmd, hdr->cmd_len);
  230. memset(sense, 0, sizeof(sense));
  231. rq->sense = sense;
  232. rq->sense_len = 0;
  233. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  234. /*
  235. * bounce this after holding a reference to the original bio, it's
  236. * needed for proper unmapping
  237. */
  238. if (rq->bio)
  239. blk_queue_bounce(q, &rq->bio);
  240. rq->timeout = (hdr->timeout * HZ) / 1000;
  241. if (!rq->timeout)
  242. rq->timeout = q->sg_timeout;
  243. if (!rq->timeout)
  244. rq->timeout = BLK_DEFAULT_TIMEOUT;
  245. if (hdr->iovec_count) {
  246. const int size = sizeof(struct sg_iovec) * hdr->iovec_count;
  247. struct sg_iovec *iov;
  248. iov = kmalloc(size, GFP_KERNEL);
  249. if (!iov) {
  250. ret = -ENOMEM;
  251. goto out;
  252. }
  253. if (copy_from_user(iov, hdr->dxferp, size)) {
  254. kfree(iov);
  255. ret = -EFAULT;
  256. goto out;
  257. }
  258. ret = blk_rq_map_user_iov(q, rq, iov, hdr->iovec_count,
  259. hdr->dxfer_len);
  260. kfree(iov);
  261. } else if (hdr->dxfer_len)
  262. ret = blk_rq_map_user(q, rq, hdr->dxferp, hdr->dxfer_len);
  263. if (ret)
  264. goto out;
  265. rq->retries = 0;
  266. start_time = jiffies;
  267. /* ignore return value. All information is passed back to caller
  268. * (if he doesn't check that is his problem).
  269. * N.B. a non-zero SCSI status is _not_ necessarily an error.
  270. */
  271. blk_execute_rq(q, bd_disk, rq, 0);
  272. /* write to all output members */
  273. hdr->status = 0xff & rq->errors;
  274. hdr->masked_status = status_byte(rq->errors);
  275. hdr->msg_status = msg_byte(rq->errors);
  276. hdr->host_status = host_byte(rq->errors);
  277. hdr->driver_status = driver_byte(rq->errors);
  278. hdr->info = 0;
  279. if (hdr->masked_status || hdr->host_status || hdr->driver_status)
  280. hdr->info |= SG_INFO_CHECK;
  281. hdr->resid = rq->data_len;
  282. hdr->duration = ((jiffies - start_time) * 1000) / HZ;
  283. hdr->sb_len_wr = 0;
  284. if (rq->sense_len && hdr->sbp) {
  285. int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
  286. if (!copy_to_user(hdr->sbp, rq->sense, len))
  287. hdr->sb_len_wr = len;
  288. }
  289. if (blk_rq_unmap_user(rq))
  290. ret = -EFAULT;
  291. /* may not have succeeded, but output values written to control
  292. * structure (struct sg_io_hdr). */
  293. out:
  294. blk_put_request(rq);
  295. return ret;
  296. }
  297. /**
  298. * sg_scsi_ioctl -- handle deprecated SCSI_IOCTL_SEND_COMMAND ioctl
  299. * @file: file this ioctl operates on (optional)
  300. * @q: request queue to send scsi commands down
  301. * @disk: gendisk to operate on (option)
  302. * @sic: userspace structure describing the command to perform
  303. *
  304. * Send down the scsi command described by @sic to the device below
  305. * the request queue @q. If @file is non-NULL it's used to perform
  306. * fine-grained permission checks that allow users to send down
  307. * non-destructive SCSI commands. If the caller has a struct gendisk
  308. * available it should be passed in as @disk to allow the low level
  309. * driver to use the information contained in it. A non-NULL @disk
  310. * is only allowed if the caller knows that the low level driver doesn't
  311. * need it (e.g. in the scsi subsystem).
  312. *
  313. * Notes:
  314. * - This interface is deprecated - users should use the SG_IO
  315. * interface instead, as this is a more flexible approach to
  316. * performing SCSI commands on a device.
  317. * - The SCSI command length is determined by examining the 1st byte
  318. * of the given command. There is no way to override this.
  319. * - Data transfers are limited to PAGE_SIZE
  320. * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
  321. * accommodate the sense buffer when an error occurs.
  322. * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
  323. * old code will not be surprised.
  324. * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
  325. * a negative return and the Unix error code in 'errno'.
  326. * If the SCSI command succeeds then 0 is returned.
  327. * Positive numbers returned are the compacted SCSI error codes (4
  328. * bytes in one int) where the lowest byte is the SCSI status.
  329. */
  330. #define OMAX_SB_LEN 16 /* For backward compatibility */
  331. int sg_scsi_ioctl(struct file *file, struct request_queue *q,
  332. struct gendisk *disk, struct scsi_ioctl_command __user *sic)
  333. {
  334. struct request *rq;
  335. int err;
  336. unsigned int in_len, out_len, bytes, opcode, cmdlen;
  337. char *buffer = NULL, sense[SCSI_SENSE_BUFFERSIZE];
  338. if (!sic)
  339. return -EINVAL;
  340. /*
  341. * get in an out lengths, verify they don't exceed a page worth of data
  342. */
  343. if (get_user(in_len, &sic->inlen))
  344. return -EFAULT;
  345. if (get_user(out_len, &sic->outlen))
  346. return -EFAULT;
  347. if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
  348. return -EINVAL;
  349. if (get_user(opcode, sic->data))
  350. return -EFAULT;
  351. bytes = max(in_len, out_len);
  352. if (bytes) {
  353. buffer = kmalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
  354. if (!buffer)
  355. return -ENOMEM;
  356. memset(buffer, 0, bytes);
  357. }
  358. rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_WAIT);
  359. cmdlen = COMMAND_SIZE(opcode);
  360. /*
  361. * get command and data to send to device, if any
  362. */
  363. err = -EFAULT;
  364. rq->cmd_len = cmdlen;
  365. if (copy_from_user(rq->cmd, sic->data, cmdlen))
  366. goto error;
  367. if (in_len && copy_from_user(buffer, sic->data + cmdlen, in_len))
  368. goto error;
  369. err = verify_command(file, rq->cmd);
  370. if (err)
  371. goto error;
  372. /* default. possible overriden later */
  373. rq->retries = 5;
  374. switch (opcode) {
  375. case SEND_DIAGNOSTIC:
  376. case FORMAT_UNIT:
  377. rq->timeout = FORMAT_UNIT_TIMEOUT;
  378. rq->retries = 1;
  379. break;
  380. case START_STOP:
  381. rq->timeout = START_STOP_TIMEOUT;
  382. break;
  383. case MOVE_MEDIUM:
  384. rq->timeout = MOVE_MEDIUM_TIMEOUT;
  385. break;
  386. case READ_ELEMENT_STATUS:
  387. rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
  388. break;
  389. case READ_DEFECT_DATA:
  390. rq->timeout = READ_DEFECT_DATA_TIMEOUT;
  391. rq->retries = 1;
  392. break;
  393. default:
  394. rq->timeout = BLK_DEFAULT_TIMEOUT;
  395. break;
  396. }
  397. if (bytes && blk_rq_map_kern(q, rq, buffer, bytes, __GFP_WAIT)) {
  398. err = DRIVER_ERROR << 24;
  399. goto out;
  400. }
  401. memset(sense, 0, sizeof(sense));
  402. rq->sense = sense;
  403. rq->sense_len = 0;
  404. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  405. blk_execute_rq(q, disk, rq, 0);
  406. out:
  407. err = rq->errors & 0xff; /* only 8 bit SCSI status */
  408. if (err) {
  409. if (rq->sense_len && rq->sense) {
  410. bytes = (OMAX_SB_LEN > rq->sense_len) ?
  411. rq->sense_len : OMAX_SB_LEN;
  412. if (copy_to_user(sic->data, rq->sense, bytes))
  413. err = -EFAULT;
  414. }
  415. } else {
  416. if (copy_to_user(sic->data, buffer, out_len))
  417. err = -EFAULT;
  418. }
  419. error:
  420. kfree(buffer);
  421. blk_put_request(rq);
  422. return err;
  423. }
  424. EXPORT_SYMBOL_GPL(sg_scsi_ioctl);
  425. /* Send basic block requests */
  426. static int __blk_send_generic(request_queue_t *q, struct gendisk *bd_disk, int cmd, int data)
  427. {
  428. struct request *rq;
  429. int err;
  430. rq = blk_get_request(q, WRITE, __GFP_WAIT);
  431. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  432. rq->data = NULL;
  433. rq->data_len = 0;
  434. rq->timeout = BLK_DEFAULT_TIMEOUT;
  435. memset(rq->cmd, 0, sizeof(rq->cmd));
  436. rq->cmd[0] = cmd;
  437. rq->cmd[4] = data;
  438. rq->cmd_len = 6;
  439. err = blk_execute_rq(q, bd_disk, rq, 0);
  440. blk_put_request(rq);
  441. return err;
  442. }
  443. static inline int blk_send_start_stop(request_queue_t *q, struct gendisk *bd_disk, int data)
  444. {
  445. return __blk_send_generic(q, bd_disk, GPCMD_START_STOP_UNIT, data);
  446. }
  447. int scsi_cmd_ioctl(struct file *file, struct gendisk *bd_disk, unsigned int cmd, void __user *arg)
  448. {
  449. request_queue_t *q;
  450. int err;
  451. q = bd_disk->queue;
  452. if (!q)
  453. return -ENXIO;
  454. if (blk_get_queue(q))
  455. return -ENXIO;
  456. switch (cmd) {
  457. /*
  458. * new sgv3 interface
  459. */
  460. case SG_GET_VERSION_NUM:
  461. err = sg_get_version(arg);
  462. break;
  463. case SCSI_IOCTL_GET_IDLUN:
  464. err = scsi_get_idlun(q, arg);
  465. break;
  466. case SCSI_IOCTL_GET_BUS_NUMBER:
  467. err = scsi_get_bus(q, arg);
  468. break;
  469. case SG_SET_TIMEOUT:
  470. err = sg_set_timeout(q, arg);
  471. break;
  472. case SG_GET_TIMEOUT:
  473. err = sg_get_timeout(q);
  474. break;
  475. case SG_GET_RESERVED_SIZE:
  476. err = sg_get_reserved_size(q, arg);
  477. break;
  478. case SG_SET_RESERVED_SIZE:
  479. err = sg_set_reserved_size(q, arg);
  480. break;
  481. case SG_EMULATED_HOST:
  482. err = sg_emulated_host(q, arg);
  483. break;
  484. case SG_IO: {
  485. struct sg_io_hdr hdr;
  486. err = -EFAULT;
  487. if (copy_from_user(&hdr, arg, sizeof(hdr)))
  488. break;
  489. err = sg_io(file, q, bd_disk, &hdr);
  490. if (err == -EFAULT)
  491. break;
  492. if (copy_to_user(arg, &hdr, sizeof(hdr)))
  493. err = -EFAULT;
  494. break;
  495. }
  496. case CDROM_SEND_PACKET: {
  497. struct cdrom_generic_command cgc;
  498. struct sg_io_hdr hdr;
  499. err = -EFAULT;
  500. if (copy_from_user(&cgc, arg, sizeof(cgc)))
  501. break;
  502. cgc.timeout = clock_t_to_jiffies(cgc.timeout);
  503. memset(&hdr, 0, sizeof(hdr));
  504. hdr.interface_id = 'S';
  505. hdr.cmd_len = sizeof(cgc.cmd);
  506. hdr.dxfer_len = cgc.buflen;
  507. err = 0;
  508. switch (cgc.data_direction) {
  509. case CGC_DATA_UNKNOWN:
  510. hdr.dxfer_direction = SG_DXFER_UNKNOWN;
  511. break;
  512. case CGC_DATA_WRITE:
  513. hdr.dxfer_direction = SG_DXFER_TO_DEV;
  514. break;
  515. case CGC_DATA_READ:
  516. hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  517. break;
  518. case CGC_DATA_NONE:
  519. hdr.dxfer_direction = SG_DXFER_NONE;
  520. break;
  521. default:
  522. err = -EINVAL;
  523. }
  524. if (err)
  525. break;
  526. hdr.dxferp = cgc.buffer;
  527. hdr.sbp = cgc.sense;
  528. if (hdr.sbp)
  529. hdr.mx_sb_len = sizeof(struct request_sense);
  530. hdr.timeout = cgc.timeout;
  531. hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
  532. hdr.cmd_len = sizeof(cgc.cmd);
  533. err = sg_io(file, q, bd_disk, &hdr);
  534. if (err == -EFAULT)
  535. break;
  536. if (hdr.status)
  537. err = -EIO;
  538. cgc.stat = err;
  539. cgc.buflen = hdr.resid;
  540. if (copy_to_user(arg, &cgc, sizeof(cgc)))
  541. err = -EFAULT;
  542. break;
  543. }
  544. /*
  545. * old junk scsi send command ioctl
  546. */
  547. case SCSI_IOCTL_SEND_COMMAND:
  548. printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
  549. err = -EINVAL;
  550. if (!arg)
  551. break;
  552. err = sg_scsi_ioctl(file, q, bd_disk, arg);
  553. break;
  554. case CDROMCLOSETRAY:
  555. err = blk_send_start_stop(q, bd_disk, 0x03);
  556. break;
  557. case CDROMEJECT:
  558. err = blk_send_start_stop(q, bd_disk, 0x02);
  559. break;
  560. default:
  561. err = -ENOTTY;
  562. }
  563. blk_put_queue(q);
  564. return err;
  565. }
  566. EXPORT_SYMBOL(scsi_cmd_ioctl);