uas.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. * USB Attached SCSI
  3. * Note that this is not the same as the USB Mass Storage driver
  4. *
  5. * Copyright Matthew Wilcox for Intel Corp, 2010
  6. * Copyright Sarah Sharp for Intel Corp, 2010
  7. *
  8. * Distributed under the terms of the GNU GPL, version two.
  9. */
  10. #include <linux/blkdev.h>
  11. #include <linux/slab.h>
  12. #include <linux/types.h>
  13. #include <linux/module.h>
  14. #include <linux/usb.h>
  15. #include <linux/usb/storage.h>
  16. #include <scsi/scsi.h>
  17. #include <scsi/scsi_dbg.h>
  18. #include <scsi/scsi_cmnd.h>
  19. #include <scsi/scsi_device.h>
  20. #include <scsi/scsi_host.h>
  21. #include <scsi/scsi_tcq.h>
  22. /* Common header for all IUs */
  23. struct iu {
  24. __u8 iu_id;
  25. __u8 rsvd1;
  26. __be16 tag;
  27. };
  28. enum {
  29. IU_ID_COMMAND = 0x01,
  30. IU_ID_STATUS = 0x03,
  31. IU_ID_RESPONSE = 0x04,
  32. IU_ID_TASK_MGMT = 0x05,
  33. IU_ID_READ_READY = 0x06,
  34. IU_ID_WRITE_READY = 0x07,
  35. };
  36. struct command_iu {
  37. __u8 iu_id;
  38. __u8 rsvd1;
  39. __be16 tag;
  40. __u8 prio_attr;
  41. __u8 rsvd5;
  42. __u8 len;
  43. __u8 rsvd7;
  44. struct scsi_lun lun;
  45. __u8 cdb[16]; /* XXX: Overflow-checking tools may misunderstand */
  46. };
  47. /*
  48. * Also used for the Read Ready and Write Ready IUs since they have the
  49. * same first four bytes
  50. */
  51. struct sense_iu {
  52. __u8 iu_id;
  53. __u8 rsvd1;
  54. __be16 tag;
  55. __be16 status_qual;
  56. __u8 status;
  57. __u8 rsvd7[7];
  58. __be16 len;
  59. __u8 sense[SCSI_SENSE_BUFFERSIZE];
  60. };
  61. /*
  62. * The r00-r01c specs define this version of the SENSE IU data structure.
  63. * It's still in use by several different firmware releases.
  64. */
  65. struct sense_iu_old {
  66. __u8 iu_id;
  67. __u8 rsvd1;
  68. __be16 tag;
  69. __be16 len;
  70. __u8 status;
  71. __u8 service_response;
  72. __u8 sense[SCSI_SENSE_BUFFERSIZE];
  73. };
  74. enum {
  75. CMD_PIPE_ID = 1,
  76. STATUS_PIPE_ID = 2,
  77. DATA_IN_PIPE_ID = 3,
  78. DATA_OUT_PIPE_ID = 4,
  79. UAS_SIMPLE_TAG = 0,
  80. UAS_HEAD_TAG = 1,
  81. UAS_ORDERED_TAG = 2,
  82. UAS_ACA = 4,
  83. };
  84. struct uas_dev_info {
  85. struct usb_interface *intf;
  86. struct usb_device *udev;
  87. int qdepth;
  88. unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
  89. unsigned use_streams:1;
  90. unsigned uas_sense_old:1;
  91. };
  92. enum {
  93. ALLOC_STATUS_URB = (1 << 0),
  94. SUBMIT_STATUS_URB = (1 << 1),
  95. ALLOC_DATA_IN_URB = (1 << 2),
  96. SUBMIT_DATA_IN_URB = (1 << 3),
  97. ALLOC_DATA_OUT_URB = (1 << 4),
  98. SUBMIT_DATA_OUT_URB = (1 << 5),
  99. ALLOC_CMD_URB = (1 << 6),
  100. SUBMIT_CMD_URB = (1 << 7),
  101. };
  102. /* Overrides scsi_pointer */
  103. struct uas_cmd_info {
  104. unsigned int state;
  105. unsigned int stream;
  106. struct urb *cmd_urb;
  107. struct urb *status_urb;
  108. struct urb *data_in_urb;
  109. struct urb *data_out_urb;
  110. struct list_head list;
  111. };
  112. /* I hate forward declarations, but I actually have a loop */
  113. static int uas_submit_urbs(struct scsi_cmnd *cmnd,
  114. struct uas_dev_info *devinfo, gfp_t gfp);
  115. static DEFINE_SPINLOCK(uas_work_lock);
  116. static LIST_HEAD(uas_work_list);
  117. static void uas_do_work(struct work_struct *work)
  118. {
  119. struct uas_cmd_info *cmdinfo;
  120. struct list_head list;
  121. spin_lock_irq(&uas_work_lock);
  122. list_replace_init(&uas_work_list, &list);
  123. spin_unlock_irq(&uas_work_lock);
  124. list_for_each_entry(cmdinfo, &list, list) {
  125. struct scsi_pointer *scp = (void *)cmdinfo;
  126. struct scsi_cmnd *cmnd = container_of(scp,
  127. struct scsi_cmnd, SCp);
  128. uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
  129. }
  130. }
  131. static DECLARE_WORK(uas_work, uas_do_work);
  132. static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
  133. {
  134. struct sense_iu *sense_iu = urb->transfer_buffer;
  135. struct scsi_device *sdev = cmnd->device;
  136. if (urb->actual_length > 16) {
  137. unsigned len = be16_to_cpup(&sense_iu->len);
  138. if (len + 16 != urb->actual_length) {
  139. int newlen = min(len + 16, urb->actual_length) - 16;
  140. if (newlen < 0)
  141. newlen = 0;
  142. sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
  143. "disagrees with IU sense data length %d, "
  144. "using %d bytes of sense data\n", __func__,
  145. urb->actual_length, len, newlen);
  146. len = newlen;
  147. }
  148. memcpy(cmnd->sense_buffer, sense_iu->sense, len);
  149. }
  150. cmnd->result = sense_iu->status;
  151. if (sdev->current_cmnd)
  152. sdev->current_cmnd = NULL;
  153. cmnd->scsi_done(cmnd);
  154. usb_free_urb(urb);
  155. }
  156. static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
  157. {
  158. struct sense_iu_old *sense_iu = urb->transfer_buffer;
  159. struct scsi_device *sdev = cmnd->device;
  160. if (urb->actual_length > 8) {
  161. unsigned len = be16_to_cpup(&sense_iu->len) - 2;
  162. if (len + 8 != urb->actual_length) {
  163. int newlen = min(len + 8, urb->actual_length) - 8;
  164. if (newlen < 0)
  165. newlen = 0;
  166. sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
  167. "disagrees with IU sense data length %d, "
  168. "using %d bytes of sense data\n", __func__,
  169. urb->actual_length, len, newlen);
  170. len = newlen;
  171. }
  172. memcpy(cmnd->sense_buffer, sense_iu->sense, len);
  173. }
  174. cmnd->result = sense_iu->status;
  175. if (sdev->current_cmnd)
  176. sdev->current_cmnd = NULL;
  177. cmnd->scsi_done(cmnd);
  178. usb_free_urb(urb);
  179. }
  180. static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
  181. unsigned direction)
  182. {
  183. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  184. int err;
  185. cmdinfo->state = direction | SUBMIT_STATUS_URB;
  186. err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
  187. if (err) {
  188. spin_lock(&uas_work_lock);
  189. list_add_tail(&cmdinfo->list, &uas_work_list);
  190. spin_unlock(&uas_work_lock);
  191. schedule_work(&uas_work);
  192. }
  193. }
  194. static void uas_stat_cmplt(struct urb *urb)
  195. {
  196. struct iu *iu = urb->transfer_buffer;
  197. struct scsi_device *sdev = urb->context;
  198. struct uas_dev_info *devinfo = sdev->hostdata;
  199. struct scsi_cmnd *cmnd;
  200. u16 tag;
  201. if (urb->status) {
  202. dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
  203. usb_free_urb(urb);
  204. return;
  205. }
  206. tag = be16_to_cpup(&iu->tag) - 1;
  207. if (sdev->current_cmnd)
  208. cmnd = sdev->current_cmnd;
  209. else
  210. cmnd = scsi_find_tag(sdev, tag);
  211. if (!cmnd)
  212. return;
  213. switch (iu->iu_id) {
  214. case IU_ID_STATUS:
  215. if (urb->actual_length < 16)
  216. devinfo->uas_sense_old = 1;
  217. if (devinfo->uas_sense_old)
  218. uas_sense_old(urb, cmnd);
  219. else
  220. uas_sense(urb, cmnd);
  221. break;
  222. case IU_ID_READ_READY:
  223. uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
  224. break;
  225. case IU_ID_WRITE_READY:
  226. uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
  227. break;
  228. default:
  229. scmd_printk(KERN_ERR, cmnd,
  230. "Bogus IU (%d) received on status pipe\n", iu->iu_id);
  231. }
  232. }
  233. static void uas_data_cmplt(struct urb *urb)
  234. {
  235. struct scsi_data_buffer *sdb = urb->context;
  236. sdb->resid = sdb->length - urb->actual_length;
  237. usb_free_urb(urb);
  238. }
  239. static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  240. unsigned int pipe, u16 stream_id,
  241. struct scsi_data_buffer *sdb,
  242. enum dma_data_direction dir)
  243. {
  244. struct usb_device *udev = devinfo->udev;
  245. struct urb *urb = usb_alloc_urb(0, gfp);
  246. if (!urb)
  247. goto out;
  248. usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, uas_data_cmplt,
  249. sdb);
  250. if (devinfo->use_streams)
  251. urb->stream_id = stream_id;
  252. urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
  253. urb->sg = sdb->table.sgl;
  254. out:
  255. return urb;
  256. }
  257. static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  258. struct scsi_cmnd *cmnd, u16 stream_id)
  259. {
  260. struct usb_device *udev = devinfo->udev;
  261. struct urb *urb = usb_alloc_urb(0, gfp);
  262. struct sense_iu *iu;
  263. if (!urb)
  264. goto out;
  265. iu = kzalloc(sizeof(*iu), gfp);
  266. if (!iu)
  267. goto free;
  268. usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
  269. uas_stat_cmplt, cmnd->device);
  270. urb->stream_id = stream_id;
  271. urb->transfer_flags |= URB_FREE_BUFFER;
  272. out:
  273. return urb;
  274. free:
  275. usb_free_urb(urb);
  276. return NULL;
  277. }
  278. static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  279. struct scsi_cmnd *cmnd, u16 stream_id)
  280. {
  281. struct usb_device *udev = devinfo->udev;
  282. struct scsi_device *sdev = cmnd->device;
  283. struct urb *urb = usb_alloc_urb(0, gfp);
  284. struct command_iu *iu;
  285. int len;
  286. if (!urb)
  287. goto out;
  288. len = cmnd->cmd_len - 16;
  289. if (len < 0)
  290. len = 0;
  291. len = ALIGN(len, 4);
  292. iu = kzalloc(sizeof(*iu) + len, gfp);
  293. if (!iu)
  294. goto free;
  295. iu->iu_id = IU_ID_COMMAND;
  296. iu->tag = cpu_to_be16(stream_id);
  297. iu->prio_attr = UAS_SIMPLE_TAG;
  298. iu->len = len;
  299. int_to_scsilun(sdev->lun, &iu->lun);
  300. memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
  301. usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
  302. usb_free_urb, NULL);
  303. urb->transfer_flags |= URB_FREE_BUFFER;
  304. out:
  305. return urb;
  306. free:
  307. usb_free_urb(urb);
  308. return NULL;
  309. }
  310. /*
  311. * Why should I request the Status IU before sending the Command IU? Spec
  312. * says to, but also says the device may receive them in any order. Seems
  313. * daft to me.
  314. */
  315. static int uas_submit_urbs(struct scsi_cmnd *cmnd,
  316. struct uas_dev_info *devinfo, gfp_t gfp)
  317. {
  318. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  319. if (cmdinfo->state & ALLOC_STATUS_URB) {
  320. cmdinfo->status_urb = uas_alloc_sense_urb(devinfo, gfp, cmnd,
  321. cmdinfo->stream);
  322. if (!cmdinfo->status_urb)
  323. return SCSI_MLQUEUE_DEVICE_BUSY;
  324. cmdinfo->state &= ~ALLOC_STATUS_URB;
  325. }
  326. if (cmdinfo->state & SUBMIT_STATUS_URB) {
  327. if (usb_submit_urb(cmdinfo->status_urb, gfp)) {
  328. scmd_printk(KERN_INFO, cmnd,
  329. "sense urb submission failure\n");
  330. return SCSI_MLQUEUE_DEVICE_BUSY;
  331. }
  332. cmdinfo->state &= ~SUBMIT_STATUS_URB;
  333. }
  334. if (cmdinfo->state & ALLOC_DATA_IN_URB) {
  335. cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
  336. devinfo->data_in_pipe, cmdinfo->stream,
  337. scsi_in(cmnd), DMA_FROM_DEVICE);
  338. if (!cmdinfo->data_in_urb)
  339. return SCSI_MLQUEUE_DEVICE_BUSY;
  340. cmdinfo->state &= ~ALLOC_DATA_IN_URB;
  341. }
  342. if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
  343. if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
  344. scmd_printk(KERN_INFO, cmnd,
  345. "data in urb submission failure\n");
  346. return SCSI_MLQUEUE_DEVICE_BUSY;
  347. }
  348. cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
  349. }
  350. if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
  351. cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
  352. devinfo->data_out_pipe, cmdinfo->stream,
  353. scsi_out(cmnd), DMA_TO_DEVICE);
  354. if (!cmdinfo->data_out_urb)
  355. return SCSI_MLQUEUE_DEVICE_BUSY;
  356. cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
  357. }
  358. if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
  359. if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
  360. scmd_printk(KERN_INFO, cmnd,
  361. "data out urb submission failure\n");
  362. return SCSI_MLQUEUE_DEVICE_BUSY;
  363. }
  364. cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
  365. }
  366. if (cmdinfo->state & ALLOC_CMD_URB) {
  367. cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
  368. cmdinfo->stream);
  369. if (!cmdinfo->cmd_urb)
  370. return SCSI_MLQUEUE_DEVICE_BUSY;
  371. cmdinfo->state &= ~ALLOC_CMD_URB;
  372. }
  373. if (cmdinfo->state & SUBMIT_CMD_URB) {
  374. if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
  375. scmd_printk(KERN_INFO, cmnd,
  376. "cmd urb submission failure\n");
  377. return SCSI_MLQUEUE_DEVICE_BUSY;
  378. }
  379. cmdinfo->state &= ~SUBMIT_CMD_URB;
  380. }
  381. return 0;
  382. }
  383. static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
  384. void (*done)(struct scsi_cmnd *))
  385. {
  386. struct scsi_device *sdev = cmnd->device;
  387. struct uas_dev_info *devinfo = sdev->hostdata;
  388. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  389. int err;
  390. BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
  391. if (!cmdinfo->status_urb && sdev->current_cmnd)
  392. return SCSI_MLQUEUE_DEVICE_BUSY;
  393. if (blk_rq_tagged(cmnd->request)) {
  394. cmdinfo->stream = cmnd->request->tag + 1;
  395. } else {
  396. sdev->current_cmnd = cmnd;
  397. cmdinfo->stream = 1;
  398. }
  399. cmnd->scsi_done = done;
  400. cmdinfo->state = ALLOC_STATUS_URB | SUBMIT_STATUS_URB |
  401. ALLOC_CMD_URB | SUBMIT_CMD_URB;
  402. switch (cmnd->sc_data_direction) {
  403. case DMA_FROM_DEVICE:
  404. cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
  405. break;
  406. case DMA_BIDIRECTIONAL:
  407. cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
  408. case DMA_TO_DEVICE:
  409. cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
  410. case DMA_NONE:
  411. break;
  412. }
  413. if (!devinfo->use_streams) {
  414. cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
  415. cmdinfo->stream = 0;
  416. }
  417. err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
  418. if (err) {
  419. /* If we did nothing, give up now */
  420. if (cmdinfo->state & SUBMIT_STATUS_URB) {
  421. usb_free_urb(cmdinfo->status_urb);
  422. return SCSI_MLQUEUE_DEVICE_BUSY;
  423. }
  424. spin_lock(&uas_work_lock);
  425. list_add_tail(&cmdinfo->list, &uas_work_list);
  426. spin_unlock(&uas_work_lock);
  427. schedule_work(&uas_work);
  428. }
  429. return 0;
  430. }
  431. static DEF_SCSI_QCMD(uas_queuecommand)
  432. static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
  433. {
  434. struct scsi_device *sdev = cmnd->device;
  435. sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
  436. cmnd->request->tag);
  437. /* XXX: Send ABORT TASK Task Management command */
  438. return FAILED;
  439. }
  440. static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
  441. {
  442. struct scsi_device *sdev = cmnd->device;
  443. sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
  444. cmnd->request->tag);
  445. /* XXX: Send LOGICAL UNIT RESET Task Management command */
  446. return FAILED;
  447. }
  448. static int uas_eh_target_reset_handler(struct scsi_cmnd *cmnd)
  449. {
  450. struct scsi_device *sdev = cmnd->device;
  451. sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
  452. cmnd->request->tag);
  453. /* XXX: Can we reset just the one USB interface?
  454. * Would calling usb_set_interface() have the right effect?
  455. */
  456. return FAILED;
  457. }
  458. static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
  459. {
  460. struct scsi_device *sdev = cmnd->device;
  461. struct uas_dev_info *devinfo = sdev->hostdata;
  462. struct usb_device *udev = devinfo->udev;
  463. sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
  464. cmnd->request->tag);
  465. if (usb_reset_device(udev))
  466. return SUCCESS;
  467. return FAILED;
  468. }
  469. static int uas_slave_alloc(struct scsi_device *sdev)
  470. {
  471. sdev->hostdata = (void *)sdev->host->hostdata[0];
  472. return 0;
  473. }
  474. static int uas_slave_configure(struct scsi_device *sdev)
  475. {
  476. struct uas_dev_info *devinfo = sdev->hostdata;
  477. scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
  478. scsi_activate_tcq(sdev, devinfo->qdepth - 1);
  479. return 0;
  480. }
  481. static struct scsi_host_template uas_host_template = {
  482. .module = THIS_MODULE,
  483. .name = "uas",
  484. .queuecommand = uas_queuecommand,
  485. .slave_alloc = uas_slave_alloc,
  486. .slave_configure = uas_slave_configure,
  487. .eh_abort_handler = uas_eh_abort_handler,
  488. .eh_device_reset_handler = uas_eh_device_reset_handler,
  489. .eh_target_reset_handler = uas_eh_target_reset_handler,
  490. .eh_bus_reset_handler = uas_eh_bus_reset_handler,
  491. .can_queue = 65536, /* Is there a limit on the _host_ ? */
  492. .this_id = -1,
  493. .sg_tablesize = SG_NONE,
  494. .cmd_per_lun = 1, /* until we override it */
  495. .skip_settle_delay = 1,
  496. .ordered_tag = 1,
  497. };
  498. static struct usb_device_id uas_usb_ids[] = {
  499. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
  500. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
  501. /* 0xaa is a prototype device I happen to have access to */
  502. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
  503. { }
  504. };
  505. MODULE_DEVICE_TABLE(usb, uas_usb_ids);
  506. static int uas_is_interface(struct usb_host_interface *intf)
  507. {
  508. return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
  509. intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
  510. intf->desc.bInterfaceProtocol == USB_PR_UAS);
  511. }
  512. static int uas_switch_interface(struct usb_device *udev,
  513. struct usb_interface *intf)
  514. {
  515. int i;
  516. if (uas_is_interface(intf->cur_altsetting))
  517. return 0;
  518. for (i = 0; i < intf->num_altsetting; i++) {
  519. struct usb_host_interface *alt = &intf->altsetting[i];
  520. if (alt == intf->cur_altsetting)
  521. continue;
  522. if (uas_is_interface(alt))
  523. return usb_set_interface(udev,
  524. alt->desc.bInterfaceNumber,
  525. alt->desc.bAlternateSetting);
  526. }
  527. return -ENODEV;
  528. }
  529. static void uas_configure_endpoints(struct uas_dev_info *devinfo)
  530. {
  531. struct usb_host_endpoint *eps[4] = { };
  532. struct usb_interface *intf = devinfo->intf;
  533. struct usb_device *udev = devinfo->udev;
  534. struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
  535. unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
  536. devinfo->uas_sense_old = 0;
  537. for (i = 0; i < n_endpoints; i++) {
  538. unsigned char *extra = endpoint[i].extra;
  539. int len = endpoint[i].extralen;
  540. while (len > 1) {
  541. if (extra[1] == USB_DT_PIPE_USAGE) {
  542. unsigned pipe_id = extra[2];
  543. if (pipe_id > 0 && pipe_id < 5)
  544. eps[pipe_id - 1] = &endpoint[i];
  545. break;
  546. }
  547. len -= extra[0];
  548. extra += extra[0];
  549. }
  550. }
  551. /*
  552. * Assume that if we didn't find a control pipe descriptor, we're
  553. * using a device with old firmware that happens to be set up like
  554. * this.
  555. */
  556. if (!eps[0]) {
  557. devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
  558. devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
  559. devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
  560. devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
  561. eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
  562. eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
  563. eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
  564. } else {
  565. devinfo->cmd_pipe = usb_sndbulkpipe(udev,
  566. eps[0]->desc.bEndpointAddress);
  567. devinfo->status_pipe = usb_rcvbulkpipe(udev,
  568. eps[1]->desc.bEndpointAddress);
  569. devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
  570. eps[2]->desc.bEndpointAddress);
  571. devinfo->data_out_pipe = usb_sndbulkpipe(udev,
  572. eps[3]->desc.bEndpointAddress);
  573. }
  574. devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
  575. GFP_KERNEL);
  576. if (devinfo->qdepth < 0) {
  577. devinfo->qdepth = 256;
  578. devinfo->use_streams = 0;
  579. } else {
  580. devinfo->use_streams = 1;
  581. }
  582. }
  583. /*
  584. * XXX: What I'd like to do here is register a SCSI host for each USB host in
  585. * the system. Follow usb-storage's design of registering a SCSI host for
  586. * each USB device for the moment. Can implement this by walking up the
  587. * USB hierarchy until we find a USB host.
  588. */
  589. static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
  590. {
  591. int result;
  592. struct Scsi_Host *shost;
  593. struct uas_dev_info *devinfo;
  594. struct usb_device *udev = interface_to_usbdev(intf);
  595. if (uas_switch_interface(udev, intf))
  596. return -ENODEV;
  597. devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
  598. if (!devinfo)
  599. return -ENOMEM;
  600. result = -ENOMEM;
  601. shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
  602. if (!shost)
  603. goto free;
  604. shost->max_cmd_len = 16 + 252;
  605. shost->max_id = 1;
  606. shost->sg_tablesize = udev->bus->sg_tablesize;
  607. result = scsi_add_host(shost, &intf->dev);
  608. if (result)
  609. goto free;
  610. shost->hostdata[0] = (unsigned long)devinfo;
  611. devinfo->intf = intf;
  612. devinfo->udev = udev;
  613. uas_configure_endpoints(devinfo);
  614. scsi_scan_host(shost);
  615. usb_set_intfdata(intf, shost);
  616. return result;
  617. free:
  618. kfree(devinfo);
  619. if (shost)
  620. scsi_host_put(shost);
  621. return result;
  622. }
  623. static int uas_pre_reset(struct usb_interface *intf)
  624. {
  625. /* XXX: Need to return 1 if it's not our device in error handling */
  626. return 0;
  627. }
  628. static int uas_post_reset(struct usb_interface *intf)
  629. {
  630. /* XXX: Need to return 1 if it's not our device in error handling */
  631. return 0;
  632. }
  633. static void uas_disconnect(struct usb_interface *intf)
  634. {
  635. struct usb_device *udev = interface_to_usbdev(intf);
  636. struct usb_host_endpoint *eps[3];
  637. struct Scsi_Host *shost = usb_get_intfdata(intf);
  638. struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
  639. scsi_remove_host(shost);
  640. eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
  641. eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
  642. eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
  643. usb_free_streams(intf, eps, 3, GFP_KERNEL);
  644. kfree(devinfo);
  645. }
  646. /*
  647. * XXX: Should this plug into libusual so we can auto-upgrade devices from
  648. * Bulk-Only to UAS?
  649. */
  650. static struct usb_driver uas_driver = {
  651. .name = "uas",
  652. .probe = uas_probe,
  653. .disconnect = uas_disconnect,
  654. .pre_reset = uas_pre_reset,
  655. .post_reset = uas_post_reset,
  656. .id_table = uas_usb_ids,
  657. };
  658. static int uas_init(void)
  659. {
  660. return usb_register(&uas_driver);
  661. }
  662. static void uas_exit(void)
  663. {
  664. usb_deregister(&uas_driver);
  665. }
  666. module_init(uas_init);
  667. module_exit(uas_exit);
  668. MODULE_LICENSE("GPL");
  669. MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");