scsi_ioctl.c 17 KB

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