scsi_ioctl.c 17 KB

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