scsi_ioctl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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, timeout;
  196. int writing = 0, ret = 0;
  197. struct request *rq;
  198. char sense[SCSI_SENSE_BUFFERSIZE];
  199. unsigned char cmd[BLK_MAX_CDB];
  200. struct bio *bio;
  201. if (hdr->interface_id != 'S')
  202. return -EINVAL;
  203. if (hdr->cmd_len > BLK_MAX_CDB)
  204. return -EINVAL;
  205. if (copy_from_user(cmd, hdr->cmdp, hdr->cmd_len))
  206. return -EFAULT;
  207. if (verify_command(file, cmd))
  208. return -EPERM;
  209. if (hdr->dxfer_len > (q->max_hw_sectors << 9))
  210. return -EIO;
  211. if (hdr->dxfer_len)
  212. switch (hdr->dxfer_direction) {
  213. default:
  214. return -EINVAL;
  215. case SG_DXFER_TO_DEV:
  216. writing = 1;
  217. break;
  218. case SG_DXFER_TO_FROM_DEV:
  219. case SG_DXFER_FROM_DEV:
  220. break;
  221. }
  222. rq = blk_get_request(q, writing ? WRITE : READ, GFP_KERNEL);
  223. if (!rq)
  224. return -ENOMEM;
  225. /*
  226. * fill in request structure
  227. */
  228. rq->cmd_len = hdr->cmd_len;
  229. memset(rq->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
  230. memcpy(rq->cmd, cmd, hdr->cmd_len);
  231. memset(sense, 0, sizeof(sense));
  232. rq->sense = sense;
  233. rq->sense_len = 0;
  234. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  235. timeout = msecs_to_jiffies(hdr->timeout);
  236. rq->timeout = (timeout < INT_MAX) ? timeout : INT_MAX;
  237. if (!rq->timeout)
  238. rq->timeout = q->sg_timeout;
  239. if (!rq->timeout)
  240. rq->timeout = BLK_DEFAULT_TIMEOUT;
  241. if (hdr->iovec_count) {
  242. const int size = sizeof(struct sg_iovec) * hdr->iovec_count;
  243. struct sg_iovec *iov;
  244. iov = kmalloc(size, GFP_KERNEL);
  245. if (!iov) {
  246. ret = -ENOMEM;
  247. goto out;
  248. }
  249. if (copy_from_user(iov, hdr->dxferp, size)) {
  250. kfree(iov);
  251. ret = -EFAULT;
  252. goto out;
  253. }
  254. ret = blk_rq_map_user_iov(q, rq, iov, hdr->iovec_count,
  255. hdr->dxfer_len);
  256. kfree(iov);
  257. } else if (hdr->dxfer_len)
  258. ret = blk_rq_map_user(q, rq, hdr->dxferp, hdr->dxfer_len);
  259. if (ret)
  260. goto out;
  261. bio = rq->bio;
  262. rq->retries = 0;
  263. start_time = jiffies;
  264. /* ignore return value. All information is passed back to caller
  265. * (if he doesn't check that is his problem).
  266. * N.B. a non-zero SCSI status is _not_ necessarily an error.
  267. */
  268. blk_execute_rq(q, bd_disk, rq, 0);
  269. /* write to all output members */
  270. hdr->status = 0xff & rq->errors;
  271. hdr->masked_status = status_byte(rq->errors);
  272. hdr->msg_status = msg_byte(rq->errors);
  273. hdr->host_status = host_byte(rq->errors);
  274. hdr->driver_status = driver_byte(rq->errors);
  275. hdr->info = 0;
  276. if (hdr->masked_status || hdr->host_status || hdr->driver_status)
  277. hdr->info |= SG_INFO_CHECK;
  278. hdr->resid = rq->data_len;
  279. hdr->duration = ((jiffies - start_time) * 1000) / HZ;
  280. hdr->sb_len_wr = 0;
  281. if (rq->sense_len && hdr->sbp) {
  282. int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
  283. if (!copy_to_user(hdr->sbp, rq->sense, len))
  284. hdr->sb_len_wr = len;
  285. }
  286. if (blk_rq_unmap_user(bio))
  287. ret = -EFAULT;
  288. /* may not have succeeded, but output values written to control
  289. * structure (struct sg_io_hdr). */
  290. out:
  291. blk_put_request(rq);
  292. return ret;
  293. }
  294. /**
  295. * sg_scsi_ioctl -- handle deprecated SCSI_IOCTL_SEND_COMMAND ioctl
  296. * @file: file this ioctl operates on (optional)
  297. * @q: request queue to send scsi commands down
  298. * @disk: gendisk to operate on (option)
  299. * @sic: userspace structure describing the command to perform
  300. *
  301. * Send down the scsi command described by @sic to the device below
  302. * the request queue @q. If @file is non-NULL it's used to perform
  303. * fine-grained permission checks that allow users to send down
  304. * non-destructive SCSI commands. If the caller has a struct gendisk
  305. * available it should be passed in as @disk to allow the low level
  306. * driver to use the information contained in it. A non-NULL @disk
  307. * is only allowed if the caller knows that the low level driver doesn't
  308. * need it (e.g. in the scsi subsystem).
  309. *
  310. * Notes:
  311. * - This interface is deprecated - users should use the SG_IO
  312. * interface instead, as this is a more flexible approach to
  313. * performing SCSI commands on a device.
  314. * - The SCSI command length is determined by examining the 1st byte
  315. * of the given command. There is no way to override this.
  316. * - Data transfers are limited to PAGE_SIZE
  317. * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
  318. * accommodate the sense buffer when an error occurs.
  319. * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
  320. * old code will not be surprised.
  321. * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
  322. * a negative return and the Unix error code in 'errno'.
  323. * If the SCSI command succeeds then 0 is returned.
  324. * Positive numbers returned are the compacted SCSI error codes (4
  325. * bytes in one int) where the lowest byte is the SCSI status.
  326. */
  327. #define OMAX_SB_LEN 16 /* For backward compatibility */
  328. int sg_scsi_ioctl(struct file *file, struct request_queue *q,
  329. struct gendisk *disk, struct scsi_ioctl_command __user *sic)
  330. {
  331. struct request *rq;
  332. int err;
  333. unsigned int in_len, out_len, bytes, opcode, cmdlen;
  334. char *buffer = NULL, sense[SCSI_SENSE_BUFFERSIZE];
  335. if (!sic)
  336. return -EINVAL;
  337. /*
  338. * get in an out lengths, verify they don't exceed a page worth of data
  339. */
  340. if (get_user(in_len, &sic->inlen))
  341. return -EFAULT;
  342. if (get_user(out_len, &sic->outlen))
  343. return -EFAULT;
  344. if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
  345. return -EINVAL;
  346. if (get_user(opcode, sic->data))
  347. return -EFAULT;
  348. bytes = max(in_len, out_len);
  349. if (bytes) {
  350. buffer = kmalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
  351. if (!buffer)
  352. return -ENOMEM;
  353. memset(buffer, 0, bytes);
  354. }
  355. rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_WAIT);
  356. cmdlen = COMMAND_SIZE(opcode);
  357. /*
  358. * get command and data to send to device, if any
  359. */
  360. err = -EFAULT;
  361. rq->cmd_len = cmdlen;
  362. if (copy_from_user(rq->cmd, sic->data, cmdlen))
  363. goto error;
  364. if (in_len && copy_from_user(buffer, sic->data + cmdlen, in_len))
  365. goto error;
  366. err = verify_command(file, rq->cmd);
  367. if (err)
  368. goto error;
  369. /* default. possible overriden later */
  370. rq->retries = 5;
  371. switch (opcode) {
  372. case SEND_DIAGNOSTIC:
  373. case FORMAT_UNIT:
  374. rq->timeout = FORMAT_UNIT_TIMEOUT;
  375. rq->retries = 1;
  376. break;
  377. case START_STOP:
  378. rq->timeout = START_STOP_TIMEOUT;
  379. break;
  380. case MOVE_MEDIUM:
  381. rq->timeout = MOVE_MEDIUM_TIMEOUT;
  382. break;
  383. case READ_ELEMENT_STATUS:
  384. rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
  385. break;
  386. case READ_DEFECT_DATA:
  387. rq->timeout = READ_DEFECT_DATA_TIMEOUT;
  388. rq->retries = 1;
  389. break;
  390. default:
  391. rq->timeout = BLK_DEFAULT_TIMEOUT;
  392. break;
  393. }
  394. if (bytes && blk_rq_map_kern(q, rq, buffer, bytes, __GFP_WAIT)) {
  395. err = DRIVER_ERROR << 24;
  396. goto out;
  397. }
  398. memset(sense, 0, sizeof(sense));
  399. rq->sense = sense;
  400. rq->sense_len = 0;
  401. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  402. blk_execute_rq(q, disk, rq, 0);
  403. out:
  404. err = rq->errors & 0xff; /* only 8 bit SCSI status */
  405. if (err) {
  406. if (rq->sense_len && rq->sense) {
  407. bytes = (OMAX_SB_LEN > rq->sense_len) ?
  408. rq->sense_len : OMAX_SB_LEN;
  409. if (copy_to_user(sic->data, rq->sense, bytes))
  410. err = -EFAULT;
  411. }
  412. } else {
  413. if (copy_to_user(sic->data, buffer, out_len))
  414. err = -EFAULT;
  415. }
  416. error:
  417. kfree(buffer);
  418. blk_put_request(rq);
  419. return err;
  420. }
  421. EXPORT_SYMBOL_GPL(sg_scsi_ioctl);
  422. /* Send basic block requests */
  423. static int __blk_send_generic(request_queue_t *q, struct gendisk *bd_disk, int cmd, int data)
  424. {
  425. struct request *rq;
  426. int err;
  427. rq = blk_get_request(q, WRITE, __GFP_WAIT);
  428. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  429. rq->data = NULL;
  430. rq->data_len = 0;
  431. rq->timeout = BLK_DEFAULT_TIMEOUT;
  432. memset(rq->cmd, 0, sizeof(rq->cmd));
  433. rq->cmd[0] = cmd;
  434. rq->cmd[4] = data;
  435. rq->cmd_len = 6;
  436. err = blk_execute_rq(q, bd_disk, rq, 0);
  437. blk_put_request(rq);
  438. return err;
  439. }
  440. static inline int blk_send_start_stop(request_queue_t *q, struct gendisk *bd_disk, int data)
  441. {
  442. return __blk_send_generic(q, bd_disk, GPCMD_START_STOP_UNIT, data);
  443. }
  444. int scsi_cmd_ioctl(struct file *file, struct gendisk *bd_disk, unsigned int cmd, void __user *arg)
  445. {
  446. request_queue_t *q;
  447. int err;
  448. q = bd_disk->queue;
  449. if (!q)
  450. return -ENXIO;
  451. if (blk_get_queue(q))
  452. return -ENXIO;
  453. switch (cmd) {
  454. /*
  455. * new sgv3 interface
  456. */
  457. case SG_GET_VERSION_NUM:
  458. err = sg_get_version(arg);
  459. break;
  460. case SCSI_IOCTL_GET_IDLUN:
  461. err = scsi_get_idlun(q, arg);
  462. break;
  463. case SCSI_IOCTL_GET_BUS_NUMBER:
  464. err = scsi_get_bus(q, arg);
  465. break;
  466. case SG_SET_TIMEOUT:
  467. err = sg_set_timeout(q, arg);
  468. break;
  469. case SG_GET_TIMEOUT:
  470. err = sg_get_timeout(q);
  471. break;
  472. case SG_GET_RESERVED_SIZE:
  473. err = sg_get_reserved_size(q, arg);
  474. break;
  475. case SG_SET_RESERVED_SIZE:
  476. err = sg_set_reserved_size(q, arg);
  477. break;
  478. case SG_EMULATED_HOST:
  479. err = sg_emulated_host(q, arg);
  480. break;
  481. case SG_IO: {
  482. struct sg_io_hdr hdr;
  483. err = -EFAULT;
  484. if (copy_from_user(&hdr, arg, sizeof(hdr)))
  485. break;
  486. err = sg_io(file, q, bd_disk, &hdr);
  487. if (err == -EFAULT)
  488. break;
  489. if (copy_to_user(arg, &hdr, sizeof(hdr)))
  490. err = -EFAULT;
  491. break;
  492. }
  493. case CDROM_SEND_PACKET: {
  494. struct cdrom_generic_command cgc;
  495. struct sg_io_hdr hdr;
  496. err = -EFAULT;
  497. if (copy_from_user(&cgc, arg, sizeof(cgc)))
  498. break;
  499. cgc.timeout = clock_t_to_jiffies(cgc.timeout);
  500. memset(&hdr, 0, sizeof(hdr));
  501. hdr.interface_id = 'S';
  502. hdr.cmd_len = sizeof(cgc.cmd);
  503. hdr.dxfer_len = cgc.buflen;
  504. err = 0;
  505. switch (cgc.data_direction) {
  506. case CGC_DATA_UNKNOWN:
  507. hdr.dxfer_direction = SG_DXFER_UNKNOWN;
  508. break;
  509. case CGC_DATA_WRITE:
  510. hdr.dxfer_direction = SG_DXFER_TO_DEV;
  511. break;
  512. case CGC_DATA_READ:
  513. hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  514. break;
  515. case CGC_DATA_NONE:
  516. hdr.dxfer_direction = SG_DXFER_NONE;
  517. break;
  518. default:
  519. err = -EINVAL;
  520. }
  521. if (err)
  522. break;
  523. hdr.dxferp = cgc.buffer;
  524. hdr.sbp = cgc.sense;
  525. if (hdr.sbp)
  526. hdr.mx_sb_len = sizeof(struct request_sense);
  527. hdr.timeout = cgc.timeout;
  528. hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
  529. hdr.cmd_len = sizeof(cgc.cmd);
  530. err = sg_io(file, q, bd_disk, &hdr);
  531. if (err == -EFAULT)
  532. break;
  533. if (hdr.status)
  534. err = -EIO;
  535. cgc.stat = err;
  536. cgc.buflen = hdr.resid;
  537. if (copy_to_user(arg, &cgc, sizeof(cgc)))
  538. err = -EFAULT;
  539. break;
  540. }
  541. /*
  542. * old junk scsi send command ioctl
  543. */
  544. case SCSI_IOCTL_SEND_COMMAND:
  545. printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
  546. err = -EINVAL;
  547. if (!arg)
  548. break;
  549. err = sg_scsi_ioctl(file, q, bd_disk, arg);
  550. break;
  551. case CDROMCLOSETRAY:
  552. err = blk_send_start_stop(q, bd_disk, 0x03);
  553. break;
  554. case CDROMEJECT:
  555. err = blk_send_start_stop(q, bd_disk, 0x02);
  556. break;
  557. default:
  558. err = -ENOTTY;
  559. }
  560. blk_put_queue(q);
  561. return err;
  562. }
  563. EXPORT_SYMBOL(scsi_cmd_ioctl);