scsi_ioctl.c 16 KB

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