scsi_ioctl.c 17 KB

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