Lines Matching full:ctx

149 	struct msc_bot_ctx *ctx = usbd_class_get_private(c_data);  in msc_get_bulk_in()  local
150 struct msc_bot_desc *desc = ctx->desc; in msc_get_bulk_in()
162 struct msc_bot_ctx *ctx = usbd_class_get_private(c_data); in msc_get_bulk_out() local
163 struct msc_bot_desc *desc = ctx->desc; in msc_get_bulk_out()
174 struct msc_bot_ctx *ctx = usbd_class_get_private(c_data); in msc_queue_bulk_out_ep() local
179 if (atomic_test_and_set_bit(&ctx->bits, MSC_BULK_OUT_QUEUED)) { in msc_queue_bulk_out_ep()
196 atomic_clear_bit(&ctx->bits, MSC_BULK_OUT_QUEUED); in msc_queue_bulk_out_ep()
218 struct msc_bot_ctx *ctx = usbd_class_get_private(c_data); in msc_reset_handler() local
222 ctx->state = MSC_BBB_EXPECT_CBW; in msc_reset_handler()
223 for (i = 0; i < ctx->registered_luns; i++) { in msc_reset_handler()
224 scsi_reset(&ctx->luns[i]); in msc_reset_handler()
227 atomic_clear_bit(&ctx->bits, MSC_BULK_IN_WEDGED); in msc_reset_handler()
228 atomic_clear_bit(&ctx->bits, MSC_BULK_OUT_WEDGED); in msc_reset_handler()
231 static bool is_cbw_meaningful(struct msc_bot_ctx *const ctx) in is_cbw_meaningful() argument
233 if (ctx->cbw.bmCBWFlags & CBW_FLAGS_RESERVED_MASK) { in is_cbw_meaningful()
238 if (ctx->cbw.bCBWLUN >= ctx->registered_luns) { in is_cbw_meaningful()
243 if (ctx->cbw.bCBWCBLength < 1 || ctx->cbw.bCBWCBLength > 16) { in is_cbw_meaningful()
251 static void msc_process_read(struct msc_bot_ctx *ctx) in msc_process_read() argument
253 struct scsi_ctx *lun = &ctx->luns[ctx->cbw.bCBWLUN]; in msc_process_read()
261 if (ctx->scsi_bytes == 0) { in msc_process_read()
262 ctx->scsi_bytes = scsi_read_data(lun, ctx->scsi_buf); in msc_process_read()
263 ctx->scsi_offset = 0; in msc_process_read()
266 if (atomic_test_and_set_bit(&ctx->bits, MSC_BULK_IN_QUEUED)) { in msc_process_read()
272 ep = msc_get_bulk_in(ctx->class_node); in msc_process_read()
279 while (ctx->scsi_bytes - ctx->scsi_offset > 0) { in msc_process_read()
280 len = MIN(ctx->scsi_bytes - ctx->scsi_offset, in msc_process_read()
289 net_buf_add_mem(buf, &ctx->scsi_buf[ctx->scsi_offset], len); in msc_process_read()
291 ctx->scsi_offset += len; in msc_process_read()
293 if (ctx->scsi_bytes == ctx->scsi_offset) { in msc_process_read()
295 ctx->scsi_bytes = scsi_read_data(lun, ctx->scsi_buf); in msc_process_read()
296 ctx->scsi_offset = 0; in msc_process_read()
301 ctx->csw.dCSWDataResidue -= bytes_queued; in msc_process_read()
302 ret = usbd_ep_enqueue(ctx->class_node, buf); in msc_process_read()
306 atomic_clear_bit(&ctx->bits, MSC_BULK_IN_QUEUED); in msc_process_read()
310 static void msc_process_cbw(struct msc_bot_ctx *ctx) in msc_process_cbw() argument
312 struct scsi_ctx *lun = &ctx->luns[ctx->cbw.bCBWLUN]; in msc_process_cbw()
317 cb_len = scsi_usb_boot_cmd_len(ctx->cbw.CBWCB, ctx->cbw.bCBWCBLength); in msc_process_cbw()
318 data_len = scsi_cmd(lun, ctx->cbw.CBWCB, cb_len, ctx->scsi_buf); in msc_process_cbw()
319 ctx->scsi_bytes = data_len; in msc_process_cbw()
320 ctx->scsi_offset = 0; in msc_process_cbw()
326 __ASSERT_NO_MSG(cmd_is_data_read || ctx->scsi_bytes == 0); in msc_process_cbw()
328 if (ctx->cbw.dCBWDataTransferLength == 0) { in msc_process_cbw()
333 ctx->csw.bCSWStatus = CSW_STATUS_COMMAND_PASSED; in msc_process_cbw()
335 ctx->csw.bCSWStatus = CSW_STATUS_COMMAND_FAILED; in msc_process_cbw()
339 ctx->csw.bCSWStatus = CSW_STATUS_PHASE_ERROR; in msc_process_cbw()
342 ctx->state = MSC_BBB_SEND_CSW; in msc_process_cbw()
350 if (ctx->cbw.bmCBWFlags & CBW_FLAGS_DIRECTION_IN) { in msc_process_cbw()
352 msc_stall_bulk_in_ep(ctx->class_node); in msc_process_cbw()
355 msc_stall_bulk_out_ep(ctx->class_node); in msc_process_cbw()
359 ctx->csw.bCSWStatus = CSW_STATUS_COMMAND_PASSED; in msc_process_cbw()
361 ctx->csw.bCSWStatus = CSW_STATUS_COMMAND_FAILED; in msc_process_cbw()
364 ctx->state = MSC_BBB_SEND_CSW; in msc_process_cbw()
365 } else if (ctx->cbw.bmCBWFlags & CBW_FLAGS_DIRECTION_IN) { in msc_process_cbw()
367 if ((data_len > ctx->cbw.dCBWDataTransferLength) || in msc_process_cbw()
370 msc_stall_bulk_in_ep(ctx->class_node); in msc_process_cbw()
371 ctx->csw.bCSWStatus = CSW_STATUS_PHASE_ERROR; in msc_process_cbw()
372 ctx->state = MSC_BBB_SEND_CSW; in msc_process_cbw()
375 ctx->state = MSC_BBB_PROCESS_READ; in msc_process_cbw()
379 if ((data_len > ctx->cbw.dCBWDataTransferLength) || in msc_process_cbw()
382 msc_stall_bulk_out_ep(ctx->class_node); in msc_process_cbw()
383 ctx->csw.bCSWStatus = CSW_STATUS_PHASE_ERROR; in msc_process_cbw()
384 ctx->state = MSC_BBB_SEND_CSW; in msc_process_cbw()
387 ctx->state = MSC_BBB_PROCESS_WRITE; in msc_process_cbw()
392 static void msc_process_write(struct msc_bot_ctx *ctx, in msc_process_write() argument
396 struct scsi_ctx *lun = &ctx->luns[ctx->cbw.bCBWLUN]; in msc_process_write()
398 ctx->transferred_data += len; in msc_process_write()
402 tmp = MIN(len, sizeof(ctx->scsi_buf) - ctx->scsi_bytes); in msc_process_write()
403 memcpy(&ctx->scsi_buf[ctx->scsi_bytes], buf, tmp); in msc_process_write()
404 ctx->scsi_bytes += tmp; in msc_process_write()
411 while ((ctx->scsi_bytes >= scsi_cmd_remaining_data_len(lun)) || in msc_process_write()
412 (ctx->scsi_bytes == sizeof(ctx->scsi_buf))) { in msc_process_write()
413 tmp = scsi_write_data(lun, ctx->scsi_buf, ctx->scsi_bytes); in msc_process_write()
414 __ASSERT(tmp <= ctx->scsi_bytes, in msc_process_write()
418 ctx->scsi_bytes); in msc_process_write()
419 ctx->scsi_bytes = 0; in msc_process_write()
422 tmp, ctx->scsi_bytes); in msc_process_write()
425 ctx->csw.dCSWDataResidue -= tmp; in msc_process_write()
428 ctx->scsi_bytes = 0; in msc_process_write()
437 memmove(ctx->scsi_buf, &ctx->scsi_buf[tmp], ctx->scsi_bytes - tmp); in msc_process_write()
438 ctx->scsi_bytes -= tmp; in msc_process_write()
442 if ((ctx->transferred_data >= ctx->cbw.dCBWDataTransferLength) || in msc_process_write()
444 if (ctx->transferred_data < ctx->cbw.dCBWDataTransferLength) { in msc_process_write()
449 msc_stall_bulk_out_ep(ctx->class_node); in msc_process_write()
453 ctx->csw.bCSWStatus = CSW_STATUS_COMMAND_PASSED; in msc_process_write()
455 ctx->csw.bCSWStatus = CSW_STATUS_COMMAND_FAILED; in msc_process_write()
458 ctx->state = MSC_BBB_SEND_CSW; in msc_process_write()
462 static void msc_handle_bulk_out(struct msc_bot_ctx *ctx, in msc_handle_bulk_out() argument
465 if (ctx->state == MSC_BBB_EXPECT_CBW) { in msc_handle_bulk_out()
467 memcpy(&ctx->cbw, buf, sizeof(struct CBW)); in msc_handle_bulk_out()
471 ctx->cbw.dCBWDataTransferLength = in msc_handle_bulk_out()
472 sys_le32_to_cpu(ctx->cbw.dCBWDataTransferLength); in msc_handle_bulk_out()
474 ctx->csw.dCSWSignature = sys_cpu_to_le32(CSW_SIGNATURE); in msc_handle_bulk_out()
475 ctx->csw.dCSWTag = ctx->cbw.dCBWTag; in msc_handle_bulk_out()
476 ctx->csw.dCSWDataResidue = ctx->cbw.dCBWDataTransferLength; in msc_handle_bulk_out()
477 ctx->transferred_data = 0; in msc_handle_bulk_out()
478 if (is_cbw_meaningful(ctx)) { in msc_handle_bulk_out()
479 ctx->csw.bCSWStatus = CSW_STATUS_COMMAND_FAILED; in msc_handle_bulk_out()
480 ctx->state = MSC_BBB_PROCESS_CBW; in msc_handle_bulk_out()
487 msc_stall_bulk_in_ep(ctx->class_node); in msc_handle_bulk_out()
488 ctx->csw.bCSWStatus = CSW_STATUS_PHASE_ERROR; in msc_handle_bulk_out()
489 ctx->state = MSC_BBB_SEND_CSW; in msc_handle_bulk_out()
494 atomic_set_bit(&ctx->bits, MSC_BULK_IN_WEDGED); in msc_handle_bulk_out()
495 atomic_set_bit(&ctx->bits, MSC_BULK_OUT_WEDGED); in msc_handle_bulk_out()
496 msc_stall_bulk_in_ep(ctx->class_node); in msc_handle_bulk_out()
497 msc_stall_bulk_out_ep(ctx->class_node); in msc_handle_bulk_out()
498 ctx->state = MSC_BBB_WAIT_FOR_RESET_RECOVERY; in msc_handle_bulk_out()
500 } else if (ctx->state == MSC_BBB_PROCESS_WRITE) { in msc_handle_bulk_out()
501 msc_process_write(ctx, buf, len); in msc_handle_bulk_out()
505 static void msc_handle_bulk_in(struct msc_bot_ctx *ctx, in msc_handle_bulk_in() argument
508 if (ctx->state == MSC_BBB_WAIT_FOR_CSW_SENT) { in msc_handle_bulk_in()
510 ctx->state = MSC_BBB_EXPECT_CBW; in msc_handle_bulk_in()
511 } else if (ctx->state == MSC_BBB_PROCESS_READ) { in msc_handle_bulk_in()
512 struct scsi_ctx *lun = &ctx->luns[ctx->cbw.bCBWLUN]; in msc_handle_bulk_in()
514 ctx->transferred_data += len; in msc_handle_bulk_in()
515 if (ctx->scsi_bytes == 0) { in msc_handle_bulk_in()
516 if (ctx->csw.dCSWDataResidue > 0) { in msc_handle_bulk_in()
522 msc_stall_bulk_in_ep(ctx->class_node); in msc_handle_bulk_in()
525 ctx->csw.bCSWStatus = CSW_STATUS_COMMAND_PASSED; in msc_handle_bulk_in()
527 ctx->csw.bCSWStatus = CSW_STATUS_COMMAND_FAILED; in msc_handle_bulk_in()
529 ctx->state = MSC_BBB_SEND_CSW; in msc_handle_bulk_in()
534 static void msc_send_csw(struct msc_bot_ctx *ctx) in msc_send_csw() argument
540 if (atomic_test_and_set_bit(&ctx->bits, MSC_BULK_IN_QUEUED)) { in msc_send_csw()
547 ctx->csw.dCSWDataResidue = sys_cpu_to_le32(ctx->csw.dCSWDataResidue); in msc_send_csw()
548 ep = msc_get_bulk_in(ctx->class_node); in msc_send_csw()
555 net_buf_add_mem(buf, &ctx->csw, sizeof(ctx->csw)); in msc_send_csw()
556 ret = usbd_ep_enqueue(ctx->class_node, buf); in msc_send_csw()
560 atomic_clear_bit(&ctx->bits, MSC_BULK_IN_QUEUED); in msc_send_csw()
562 ctx->state = MSC_BBB_WAIT_FOR_CSW_SENT; in msc_send_csw()
569 struct msc_bot_ctx *ctx = usbd_class_get_private(c_data); in usbd_msc_handle_request() local
586 msc_handle_bulk_out(ctx, buf->data, buf->len); in usbd_msc_handle_request()
588 msc_handle_bulk_in(ctx, buf->data, buf->len); in usbd_msc_handle_request()
593 atomic_clear_bit(&ctx->bits, MSC_BULK_OUT_QUEUED); in usbd_msc_handle_request()
595 atomic_clear_bit(&ctx->bits, MSC_BULK_IN_QUEUED); in usbd_msc_handle_request()
606 struct msc_bot_ctx *ctx; in usbd_msc_thread() local
611 ctx = usbd_class_get_private(evt.c_data); in usbd_msc_thread()
618 if (!atomic_test_bit(&ctx->bits, MSC_CLASS_ENABLED)) { in usbd_msc_thread()
622 switch (ctx->state) { in usbd_msc_thread()
635 if (atomic_test_bit(&ctx->bits, MSC_BULK_IN_QUEUED)) { in usbd_msc_thread()
639 if (ctx->state == MSC_BBB_PROCESS_CBW) { in usbd_msc_thread()
640 msc_process_cbw(ctx); in usbd_msc_thread()
643 if (ctx->state == MSC_BBB_PROCESS_READ) { in usbd_msc_thread()
644 msc_process_read(ctx); in usbd_msc_thread()
645 } else if (ctx->state == MSC_BBB_PROCESS_WRITE) { in usbd_msc_thread()
647 } else if (ctx->state == MSC_BBB_SEND_CSW) { in usbd_msc_thread()
648 msc_send_csw(ctx); in usbd_msc_thread()
667 struct msc_bot_ctx *ctx = usbd_class_get_private(c_data); in msc_bot_feature_halt() local
670 atomic_test_bit(&ctx->bits, MSC_BULK_IN_WEDGED)) { in msc_bot_feature_halt()
674 atomic_test_bit(&ctx->bits, MSC_BULK_OUT_WEDGED)) { in msc_bot_feature_halt()
700 struct msc_bot_ctx *ctx = usbd_class_get_private(c_data); in msc_bot_control_to_host() local
709 max_lun = ctx->registered_luns ? ctx->registered_luns - 1 : 0; in msc_bot_control_to_host()
737 struct msc_bot_ctx *ctx = usbd_class_get_private(c_data); in msc_bot_enable() local
740 atomic_set_bit(&ctx->bits, MSC_CLASS_ENABLED); in msc_bot_enable()
747 struct msc_bot_ctx *ctx = usbd_class_get_private(c_data); in msc_bot_disable() local
750 atomic_clear_bit(&ctx->bits, MSC_CLASS_ENABLED); in msc_bot_disable()
756 struct msc_bot_ctx *ctx = usbd_class_get_private(c_data); in msc_bot_get_desc() local
759 return ctx->hs_desc; in msc_bot_get_desc()
762 return ctx->fs_desc; in msc_bot_get_desc()
768 struct msc_bot_ctx *ctx = usbd_class_get_private(c_data); in msc_bot_init() local
770 ctx->class_node = c_data; in msc_bot_init()
771 ctx->state = MSC_BBB_EXPECT_CBW; in msc_bot_init()
772 ctx->registered_luns = 0; in msc_bot_init()
775 if (ctx->registered_luns >= CONFIG_USBD_MSC_LUNS_PER_INSTANCE) { in msc_bot_init()
780 scsi_init(&ctx->luns[ctx->registered_luns++], lun->disk, in msc_bot_init()