scsi_ioctl.c 17 KB

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