scsi_ioctl.c 16 KB

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