uas.c 19 KB

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