scsi_ioctl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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_tbl[8] =
  35. {
  36. 6, 10, 10, 12,
  37. 16, 12, 10, 10
  38. };
  39. EXPORT_SYMBOL(scsi_command_size_tbl);
  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 jiffies_to_clock_t(q->sg_timeout);
  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 = clock_t_to_jiffies(timeout);
  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. void blk_set_cmd_filter_defaults(struct blk_cmd_filter *filter)
  91. {
  92. /* Basic read-only commands */
  93. __set_bit(TEST_UNIT_READY, filter->read_ok);
  94. __set_bit(REQUEST_SENSE, filter->read_ok);
  95. __set_bit(READ_6, filter->read_ok);
  96. __set_bit(READ_10, filter->read_ok);
  97. __set_bit(READ_12, filter->read_ok);
  98. __set_bit(READ_16, filter->read_ok);
  99. __set_bit(READ_BUFFER, filter->read_ok);
  100. __set_bit(READ_DEFECT_DATA, filter->read_ok);
  101. __set_bit(READ_CAPACITY, filter->read_ok);
  102. __set_bit(READ_LONG, filter->read_ok);
  103. __set_bit(INQUIRY, filter->read_ok);
  104. __set_bit(MODE_SENSE, filter->read_ok);
  105. __set_bit(MODE_SENSE_10, filter->read_ok);
  106. __set_bit(LOG_SENSE, filter->read_ok);
  107. __set_bit(START_STOP, filter->read_ok);
  108. __set_bit(GPCMD_VERIFY_10, filter->read_ok);
  109. __set_bit(VERIFY_16, filter->read_ok);
  110. __set_bit(REPORT_LUNS, filter->read_ok);
  111. __set_bit(SERVICE_ACTION_IN, filter->read_ok);
  112. __set_bit(RECEIVE_DIAGNOSTIC, filter->read_ok);
  113. __set_bit(MAINTENANCE_IN, filter->read_ok);
  114. __set_bit(GPCMD_READ_BUFFER_CAPACITY, filter->read_ok);
  115. /* Audio CD commands */
  116. __set_bit(GPCMD_PLAY_CD, filter->read_ok);
  117. __set_bit(GPCMD_PLAY_AUDIO_10, filter->read_ok);
  118. __set_bit(GPCMD_PLAY_AUDIO_MSF, filter->read_ok);
  119. __set_bit(GPCMD_PLAY_AUDIO_TI, filter->read_ok);
  120. __set_bit(GPCMD_PAUSE_RESUME, filter->read_ok);
  121. /* CD/DVD data reading */
  122. __set_bit(GPCMD_READ_CD, filter->read_ok);
  123. __set_bit(GPCMD_READ_CD_MSF, filter->read_ok);
  124. __set_bit(GPCMD_READ_DISC_INFO, filter->read_ok);
  125. __set_bit(GPCMD_READ_CDVD_CAPACITY, filter->read_ok);
  126. __set_bit(GPCMD_READ_DVD_STRUCTURE, filter->read_ok);
  127. __set_bit(GPCMD_READ_HEADER, filter->read_ok);
  128. __set_bit(GPCMD_READ_TRACK_RZONE_INFO, filter->read_ok);
  129. __set_bit(GPCMD_READ_SUBCHANNEL, filter->read_ok);
  130. __set_bit(GPCMD_READ_TOC_PMA_ATIP, filter->read_ok);
  131. __set_bit(GPCMD_REPORT_KEY, filter->read_ok);
  132. __set_bit(GPCMD_SCAN, filter->read_ok);
  133. __set_bit(GPCMD_GET_CONFIGURATION, filter->read_ok);
  134. __set_bit(GPCMD_READ_FORMAT_CAPACITIES, filter->read_ok);
  135. __set_bit(GPCMD_GET_EVENT_STATUS_NOTIFICATION, filter->read_ok);
  136. __set_bit(GPCMD_GET_PERFORMANCE, filter->read_ok);
  137. __set_bit(GPCMD_SEEK, filter->read_ok);
  138. __set_bit(GPCMD_STOP_PLAY_SCAN, filter->read_ok);
  139. /* Basic writing commands */
  140. __set_bit(WRITE_6, filter->write_ok);
  141. __set_bit(WRITE_10, filter->write_ok);
  142. __set_bit(WRITE_VERIFY, filter->write_ok);
  143. __set_bit(WRITE_12, filter->write_ok);
  144. __set_bit(WRITE_VERIFY_12, filter->write_ok);
  145. __set_bit(WRITE_16, filter->write_ok);
  146. __set_bit(WRITE_LONG, filter->write_ok);
  147. __set_bit(WRITE_LONG_2, filter->write_ok);
  148. __set_bit(ERASE, filter->write_ok);
  149. __set_bit(GPCMD_MODE_SELECT_10, filter->write_ok);
  150. __set_bit(MODE_SELECT, filter->write_ok);
  151. __set_bit(LOG_SELECT, filter->write_ok);
  152. __set_bit(GPCMD_BLANK, filter->write_ok);
  153. __set_bit(GPCMD_CLOSE_TRACK, filter->write_ok);
  154. __set_bit(GPCMD_FLUSH_CACHE, filter->write_ok);
  155. __set_bit(GPCMD_FORMAT_UNIT, filter->write_ok);
  156. __set_bit(GPCMD_REPAIR_RZONE_TRACK, filter->write_ok);
  157. __set_bit(GPCMD_RESERVE_RZONE_TRACK, filter->write_ok);
  158. __set_bit(GPCMD_SEND_DVD_STRUCTURE, filter->write_ok);
  159. __set_bit(GPCMD_SEND_EVENT, filter->write_ok);
  160. __set_bit(GPCMD_SEND_KEY, filter->write_ok);
  161. __set_bit(GPCMD_SEND_OPC, filter->write_ok);
  162. __set_bit(GPCMD_SEND_CUE_SHEET, filter->write_ok);
  163. __set_bit(GPCMD_SET_SPEED, filter->write_ok);
  164. __set_bit(GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL, filter->write_ok);
  165. __set_bit(GPCMD_LOAD_UNLOAD, filter->write_ok);
  166. __set_bit(GPCMD_SET_STREAMING, filter->write_ok);
  167. __set_bit(GPCMD_SET_READ_AHEAD, filter->write_ok);
  168. }
  169. EXPORT_SYMBOL_GPL(blk_set_cmd_filter_defaults);
  170. static int blk_fill_sghdr_rq(struct request_queue *q, struct request *rq,
  171. struct sg_io_hdr *hdr, fmode_t mode)
  172. {
  173. if (copy_from_user(rq->cmd, hdr->cmdp, hdr->cmd_len))
  174. return -EFAULT;
  175. if (blk_verify_command(&q->cmd_filter, rq->cmd, mode & FMODE_WRITE))
  176. return -EPERM;
  177. /*
  178. * fill in request structure
  179. */
  180. rq->cmd_len = hdr->cmd_len;
  181. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  182. rq->timeout = msecs_to_jiffies(hdr->timeout);
  183. if (!rq->timeout)
  184. rq->timeout = q->sg_timeout;
  185. if (!rq->timeout)
  186. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  187. if (rq->timeout < BLK_MIN_SG_TIMEOUT)
  188. rq->timeout = BLK_MIN_SG_TIMEOUT;
  189. return 0;
  190. }
  191. static int blk_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
  192. struct bio *bio)
  193. {
  194. int ret = 0;
  195. /*
  196. * fill in all the output members
  197. */
  198. hdr->status = rq->errors & 0xff;
  199. hdr->masked_status = status_byte(rq->errors);
  200. hdr->msg_status = msg_byte(rq->errors);
  201. hdr->host_status = host_byte(rq->errors);
  202. hdr->driver_status = driver_byte(rq->errors);
  203. hdr->info = 0;
  204. if (hdr->masked_status || hdr->host_status || hdr->driver_status)
  205. hdr->info |= SG_INFO_CHECK;
  206. hdr->resid = rq->data_len;
  207. hdr->sb_len_wr = 0;
  208. if (rq->sense_len && hdr->sbp) {
  209. int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
  210. if (!copy_to_user(hdr->sbp, rq->sense, len))
  211. hdr->sb_len_wr = len;
  212. else
  213. ret = -EFAULT;
  214. }
  215. blk_rq_unmap_user(bio);
  216. blk_put_request(rq);
  217. return ret;
  218. }
  219. static int sg_io(struct request_queue *q, struct gendisk *bd_disk,
  220. struct sg_io_hdr *hdr, fmode_t mode)
  221. {
  222. unsigned long start_time;
  223. int writing = 0, ret = 0;
  224. struct request *rq;
  225. char sense[SCSI_SENSE_BUFFERSIZE];
  226. struct bio *bio;
  227. if (hdr->interface_id != 'S')
  228. return -EINVAL;
  229. if (hdr->cmd_len > BLK_MAX_CDB)
  230. return -EINVAL;
  231. if (hdr->dxfer_len > (q->max_hw_sectors << 9))
  232. return -EIO;
  233. if (hdr->dxfer_len)
  234. switch (hdr->dxfer_direction) {
  235. default:
  236. return -EINVAL;
  237. case SG_DXFER_TO_DEV:
  238. writing = 1;
  239. break;
  240. case SG_DXFER_TO_FROM_DEV:
  241. case SG_DXFER_FROM_DEV:
  242. break;
  243. }
  244. rq = blk_get_request(q, writing ? WRITE : READ, GFP_KERNEL);
  245. if (!rq)
  246. return -ENOMEM;
  247. if (blk_fill_sghdr_rq(q, rq, hdr, mode)) {
  248. blk_put_request(rq);
  249. return -EFAULT;
  250. }
  251. if (hdr->iovec_count) {
  252. const int size = sizeof(struct sg_iovec) * hdr->iovec_count;
  253. struct sg_iovec *iov;
  254. iov = kmalloc(size, GFP_KERNEL);
  255. if (!iov) {
  256. ret = -ENOMEM;
  257. goto out;
  258. }
  259. if (copy_from_user(iov, hdr->dxferp, size)) {
  260. kfree(iov);
  261. ret = -EFAULT;
  262. goto out;
  263. }
  264. ret = blk_rq_map_user_iov(q, rq, NULL, iov, hdr->iovec_count,
  265. hdr->dxfer_len, GFP_KERNEL);
  266. kfree(iov);
  267. } else if (hdr->dxfer_len)
  268. ret = blk_rq_map_user(q, rq, NULL, hdr->dxferp, hdr->dxfer_len,
  269. GFP_KERNEL);
  270. if (ret)
  271. goto out;
  272. bio = rq->bio;
  273. memset(sense, 0, sizeof(sense));
  274. rq->sense = sense;
  275. rq->sense_len = 0;
  276. rq->retries = 0;
  277. start_time = jiffies;
  278. /* ignore return value. All information is passed back to caller
  279. * (if he doesn't check that is his problem).
  280. * N.B. a non-zero SCSI status is _not_ necessarily an error.
  281. */
  282. blk_execute_rq(q, bd_disk, rq, 0);
  283. hdr->duration = jiffies_to_msecs(jiffies - start_time);
  284. return blk_complete_sghdr_rq(rq, hdr, bio);
  285. out:
  286. blk_put_request(rq);
  287. return ret;
  288. }
  289. /**
  290. * sg_scsi_ioctl -- handle deprecated SCSI_IOCTL_SEND_COMMAND ioctl
  291. * @file: file this ioctl operates on (optional)
  292. * @q: request queue to send scsi commands down
  293. * @disk: gendisk to operate on (option)
  294. * @sic: userspace structure describing the command to perform
  295. *
  296. * Send down the scsi command described by @sic to the device below
  297. * the request queue @q. If @file is non-NULL it's used to perform
  298. * fine-grained permission checks that allow users to send down
  299. * non-destructive SCSI commands. If the caller has a struct gendisk
  300. * available it should be passed in as @disk to allow the low level
  301. * driver to use the information contained in it. A non-NULL @disk
  302. * is only allowed if the caller knows that the low level driver doesn't
  303. * need it (e.g. in the scsi subsystem).
  304. *
  305. * Notes:
  306. * - This interface is deprecated - users should use the SG_IO
  307. * interface instead, as this is a more flexible approach to
  308. * performing SCSI commands on a device.
  309. * - The SCSI command length is determined by examining the 1st byte
  310. * of the given command. There is no way to override this.
  311. * - Data transfers are limited to PAGE_SIZE
  312. * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
  313. * accommodate the sense buffer when an error occurs.
  314. * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
  315. * old code will not be surprised.
  316. * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
  317. * a negative return and the Unix error code in 'errno'.
  318. * If the SCSI command succeeds then 0 is returned.
  319. * Positive numbers returned are the compacted SCSI error codes (4
  320. * bytes in one int) where the lowest byte is the SCSI status.
  321. */
  322. #define OMAX_SB_LEN 16 /* For backward compatibility */
  323. int sg_scsi_ioctl(struct request_queue *q, struct gendisk *disk, fmode_t mode,
  324. struct scsi_ioctl_command __user *sic)
  325. {
  326. struct request *rq;
  327. int err;
  328. unsigned int in_len, out_len, bytes, opcode, cmdlen;
  329. char *buffer = NULL, sense[SCSI_SENSE_BUFFERSIZE];
  330. if (!sic)
  331. return -EINVAL;
  332. /*
  333. * get in an out lengths, verify they don't exceed a page worth of data
  334. */
  335. if (get_user(in_len, &sic->inlen))
  336. return -EFAULT;
  337. if (get_user(out_len, &sic->outlen))
  338. return -EFAULT;
  339. if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
  340. return -EINVAL;
  341. if (get_user(opcode, sic->data))
  342. return -EFAULT;
  343. bytes = max(in_len, out_len);
  344. if (bytes) {
  345. buffer = kzalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
  346. if (!buffer)
  347. return -ENOMEM;
  348. }
  349. rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_WAIT);
  350. cmdlen = COMMAND_SIZE(opcode);
  351. /*
  352. * get command and data to send to device, if any
  353. */
  354. err = -EFAULT;
  355. rq->cmd_len = cmdlen;
  356. if (copy_from_user(rq->cmd, sic->data, cmdlen))
  357. goto error;
  358. if (in_len && copy_from_user(buffer, sic->data + cmdlen, in_len))
  359. goto error;
  360. err = blk_verify_command(&q->cmd_filter, rq->cmd, mode & FMODE_WRITE);
  361. if (err)
  362. goto error;
  363. /* default. possible overriden later */
  364. rq->retries = 5;
  365. switch (opcode) {
  366. case SEND_DIAGNOSTIC:
  367. case FORMAT_UNIT:
  368. rq->timeout = FORMAT_UNIT_TIMEOUT;
  369. rq->retries = 1;
  370. break;
  371. case START_STOP:
  372. rq->timeout = START_STOP_TIMEOUT;
  373. break;
  374. case MOVE_MEDIUM:
  375. rq->timeout = MOVE_MEDIUM_TIMEOUT;
  376. break;
  377. case READ_ELEMENT_STATUS:
  378. rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
  379. break;
  380. case READ_DEFECT_DATA:
  381. rq->timeout = READ_DEFECT_DATA_TIMEOUT;
  382. rq->retries = 1;
  383. break;
  384. default:
  385. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  386. break;
  387. }
  388. if (bytes && blk_rq_map_kern(q, rq, buffer, bytes, __GFP_WAIT)) {
  389. err = DRIVER_ERROR << 24;
  390. goto out;
  391. }
  392. memset(sense, 0, sizeof(sense));
  393. rq->sense = sense;
  394. rq->sense_len = 0;
  395. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  396. blk_execute_rq(q, disk, rq, 0);
  397. out:
  398. err = rq->errors & 0xff; /* only 8 bit SCSI status */
  399. if (err) {
  400. if (rq->sense_len && rq->sense) {
  401. bytes = (OMAX_SB_LEN > rq->sense_len) ?
  402. rq->sense_len : OMAX_SB_LEN;
  403. if (copy_to_user(sic->data, rq->sense, bytes))
  404. err = -EFAULT;
  405. }
  406. } else {
  407. if (copy_to_user(sic->data, buffer, out_len))
  408. err = -EFAULT;
  409. }
  410. error:
  411. kfree(buffer);
  412. blk_put_request(rq);
  413. return err;
  414. }
  415. EXPORT_SYMBOL_GPL(sg_scsi_ioctl);
  416. /* Send basic block requests */
  417. static int __blk_send_generic(struct request_queue *q, struct gendisk *bd_disk,
  418. int cmd, int data)
  419. {
  420. struct request *rq;
  421. int err;
  422. rq = blk_get_request(q, WRITE, __GFP_WAIT);
  423. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  424. rq->data = NULL;
  425. rq->data_len = 0;
  426. rq->extra_len = 0;
  427. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  428. rq->cmd[0] = cmd;
  429. rq->cmd[4] = data;
  430. rq->cmd_len = 6;
  431. err = blk_execute_rq(q, bd_disk, rq, 0);
  432. blk_put_request(rq);
  433. return err;
  434. }
  435. static inline int blk_send_start_stop(struct request_queue *q,
  436. struct gendisk *bd_disk, int data)
  437. {
  438. return __blk_send_generic(q, bd_disk, GPCMD_START_STOP_UNIT, data);
  439. }
  440. int scsi_cmd_ioctl(struct request_queue *q, struct gendisk *bd_disk, fmode_t mode,
  441. unsigned int cmd, void __user *arg)
  442. {
  443. int err;
  444. if (!q || blk_get_queue(q))
  445. return -ENXIO;
  446. switch (cmd) {
  447. /*
  448. * new sgv3 interface
  449. */
  450. case SG_GET_VERSION_NUM:
  451. err = sg_get_version(arg);
  452. break;
  453. case SCSI_IOCTL_GET_IDLUN:
  454. err = scsi_get_idlun(q, arg);
  455. break;
  456. case SCSI_IOCTL_GET_BUS_NUMBER:
  457. err = scsi_get_bus(q, arg);
  458. break;
  459. case SG_SET_TIMEOUT:
  460. err = sg_set_timeout(q, arg);
  461. break;
  462. case SG_GET_TIMEOUT:
  463. err = sg_get_timeout(q);
  464. break;
  465. case SG_GET_RESERVED_SIZE:
  466. err = sg_get_reserved_size(q, arg);
  467. break;
  468. case SG_SET_RESERVED_SIZE:
  469. err = sg_set_reserved_size(q, arg);
  470. break;
  471. case SG_EMULATED_HOST:
  472. err = sg_emulated_host(q, arg);
  473. break;
  474. case SG_IO: {
  475. struct sg_io_hdr hdr;
  476. err = -EFAULT;
  477. if (copy_from_user(&hdr, arg, sizeof(hdr)))
  478. break;
  479. err = sg_io(q, bd_disk, &hdr, mode);
  480. if (err == -EFAULT)
  481. break;
  482. if (copy_to_user(arg, &hdr, sizeof(hdr)))
  483. err = -EFAULT;
  484. break;
  485. }
  486. case CDROM_SEND_PACKET: {
  487. struct cdrom_generic_command cgc;
  488. struct sg_io_hdr hdr;
  489. err = -EFAULT;
  490. if (copy_from_user(&cgc, arg, sizeof(cgc)))
  491. break;
  492. cgc.timeout = clock_t_to_jiffies(cgc.timeout);
  493. memset(&hdr, 0, sizeof(hdr));
  494. hdr.interface_id = 'S';
  495. hdr.cmd_len = sizeof(cgc.cmd);
  496. hdr.dxfer_len = cgc.buflen;
  497. err = 0;
  498. switch (cgc.data_direction) {
  499. case CGC_DATA_UNKNOWN:
  500. hdr.dxfer_direction = SG_DXFER_UNKNOWN;
  501. break;
  502. case CGC_DATA_WRITE:
  503. hdr.dxfer_direction = SG_DXFER_TO_DEV;
  504. break;
  505. case CGC_DATA_READ:
  506. hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  507. break;
  508. case CGC_DATA_NONE:
  509. hdr.dxfer_direction = SG_DXFER_NONE;
  510. break;
  511. default:
  512. err = -EINVAL;
  513. }
  514. if (err)
  515. break;
  516. hdr.dxferp = cgc.buffer;
  517. hdr.sbp = cgc.sense;
  518. if (hdr.sbp)
  519. hdr.mx_sb_len = sizeof(struct request_sense);
  520. hdr.timeout = jiffies_to_msecs(cgc.timeout);
  521. hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
  522. hdr.cmd_len = sizeof(cgc.cmd);
  523. err = sg_io(q, bd_disk, &hdr, mode);
  524. if (err == -EFAULT)
  525. break;
  526. if (hdr.status)
  527. err = -EIO;
  528. cgc.stat = err;
  529. cgc.buflen = hdr.resid;
  530. if (copy_to_user(arg, &cgc, sizeof(cgc)))
  531. err = -EFAULT;
  532. break;
  533. }
  534. /*
  535. * old junk scsi send command ioctl
  536. */
  537. case SCSI_IOCTL_SEND_COMMAND:
  538. printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
  539. err = -EINVAL;
  540. if (!arg)
  541. break;
  542. err = sg_scsi_ioctl(q, bd_disk, mode, arg);
  543. break;
  544. case CDROMCLOSETRAY:
  545. err = blk_send_start_stop(q, bd_disk, 0x03);
  546. break;
  547. case CDROMEJECT:
  548. err = blk_send_start_stop(q, bd_disk, 0x02);
  549. break;
  550. default:
  551. err = -ENOTTY;
  552. }
  553. blk_put_queue(q);
  554. return err;
  555. }
  556. EXPORT_SYMBOL(scsi_cmd_ioctl);