uas.c 19 KB

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