iPXE
Functions | Variables
usbblk.c File Reference

USB mass storage driver. More...

#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <ipxe/usb.h>
#include <ipxe/scsi.h>
#include <ipxe/xfer.h>
#include <ipxe/uri.h>
#include <ipxe/open.h>
#include <ipxe/efi/efi_path.h>
#include "usbblk.h"

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void usbblk_stop (struct usbblk_device *usbblk, int rc)
 Stop SCSI command. More...
 
static LIST_HEAD (usbblk_devices)
 List of USB block devices. More...
 
static int usbblk_open (struct usbblk_device *usbblk)
 Open endpoints. More...
 
static void usbblk_close (struct usbblk_device *usbblk)
 Close endpoints. More...
 
static int usbblk_out_command (struct usbblk_device *usbblk)
 Issue bulk OUT command. More...
 
static int usbblk_out_data (struct usbblk_device *usbblk)
 Send bulk OUT data block. More...
 
static int usbblk_out_refill (struct usbblk_device *usbblk)
 Refill bulk OUT endpoint. More...
 
static void usbblk_out_complete (struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
 Complete bulk OUT transfer. More...
 
static int usbblk_in_data (struct usbblk_device *usbblk, const void *data, size_t len)
 Handle bulk IN data block. More...
 
static int usbblk_in_status (struct usbblk_device *usbblk, const void *data, size_t len)
 Handle bulk IN status. More...
 
static int usbblk_in_refill (struct usbblk_device *usbblk)
 Refill bulk IN endpoint. More...
 
static void usbblk_in_complete (struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
 Complete bulk IN transfer. More...
 
static void usbblk_step (struct usbblk_device *usbblk)
 Refill endpoints. More...
 
static int usbblk_start (struct usbblk_device *usbblk, struct scsi_cmd *scsicmd)
 Start new SCSI command. More...
 
static size_t usbblk_scsi_window (struct usbblk_device *usbblk)
 Check SCSI command flow-control window. More...
 
static int usbblk_scsi_command (struct usbblk_device *usbblk, struct interface *data, struct scsi_cmd *scsicmd)
 Issue SCSI command. More...
 
static void usbblk_scsi_close (struct usbblk_device *usbblk, int rc)
 Close SCSI interface. More...
 
static EFI_DEVICE_PATH_PROTOCOLusbblk_efi_describe (struct usbblk_device *usbblk)
 Describe as an EFI device path. More...
 
static struct usbblk_deviceusbblk_find (const char *name)
 Find USB block device. More...
 
static int usbblk_open_uri (struct interface *parent, struct uri *uri)
 Open USB block device URI. More...
 
static int usbblk_probe (struct usb_function *func, struct usb_configuration_descriptor *config)
 Probe device. More...
 
static void usbblk_remove (struct usb_function *func)
 Remove device. More...
 

Variables

static struct usb_endpoint_driver_operations usbblk_out_operations
 Bulk OUT endpoint operations. More...
 
static struct usb_endpoint_driver_operations usbblk_in_operations
 Bulk IN endpoint operations. More...
 
static struct process_descriptor usbblk_process_desc
 Refill process descriptor. More...
 
static uint16_t usbblk_tag
 Next command tag. More...
 
static struct interface_operation usbblk_data_operations []
 SCSI data interface operations. More...
 
static struct interface_descriptor usbblk_data_desc
 SCSI data interface descriptor. More...
 
static struct interface_operation usbblk_scsi_operations []
 SCSI command interface operations. More...
 
static struct interface_descriptor usbblk_scsi_desc
 SCSI command interface descriptor. More...
 
struct uri_opener usbblk_uri_opener __uri_opener
 USB block device URI opener. More...
 
static struct usb_device_id usbblk_ids []
 Mass storage class device IDs. More...
 
struct usb_driver usbblk_driver __usb_driver
 Mass storage driver. More...
 

Detailed Description

USB mass storage driver.

Definition in file usbblk.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ usbblk_stop()

static void usbblk_stop ( struct usbblk_device usbblk,
int  rc 
)
static

Stop SCSI command.

Parameters
usbblkUSB block device
rcReason for stop

Definition at line 552 of file usbblk.c.

552  {
553 
554  /* Stop process */
555  process_del ( &usbblk->process );
556 
557  /* Reset command */
558  memset ( &usbblk->cmd, 0, sizeof ( usbblk->cmd ) );
559 
560  /* Close endpoints if an error occurred */
561  if ( rc != 0 ) {
562  DBGC ( usbblk, "USBBLK %s closing for error recovery\n",
563  usbblk->func->name );
564  usbblk_close ( usbblk );
565  }
566 
567  /* Terminate command */
568  intf_restart ( &usbblk->data, rc );
569 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:100
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:343
struct process process
Command process.
Definition: usbblk.h:95
const char * name
Name.
Definition: usb.h:661
#define DBGC(...)
Definition: compiler.h:505
void process_del(struct process *process)
Remove process from process list.
Definition: process.c:79
struct usb_function * func
USB function.
Definition: usbblk.h:84
static void usbblk_close(struct usbblk_device *usbblk)
Close endpoints.
Definition: usbblk.c:123
struct interface data
SCSI data interface.
Definition: usbblk.h:93
void * memset(void *dest, int character, size_t len) __nonnull

References usbblk_device::cmd, usbblk_device::data, DBGC, usbblk_device::func, intf_restart(), memset(), usb_function::name, usbblk_device::process, process_del(), rc, and usbblk_close().

Referenced by usbblk_in_complete(), usbblk_in_status(), usbblk_out_complete(), usbblk_scsi_close(), and usbblk_scsi_command().

◆ LIST_HEAD()

static LIST_HEAD ( usbblk_devices  )
static

List of USB block devices.

◆ usbblk_open()

static int usbblk_open ( struct usbblk_device usbblk)
static

Open endpoints.

Parameters
usbblkUSB block device
Return values
rcReturn status code

Definition at line 61 of file usbblk.c.

61  {
62  struct usb_device *usb = usbblk->func->usb;
63  unsigned int interface = usbblk->func->interface[0];
64  int rc;
65 
66  /* Sanity checks */
67  assert ( ! usbblk->in.open );
68  assert ( ! usbblk->out.open );
69 
70  /* Issue reset */
71  if ( ( rc = usb_control ( usb, USBBLK_RESET, 0, interface,
72  NULL, 0 ) ) != 0 ) {
73  DBGC ( usbblk, "USBBLK %s could not issue reset: %s\n",
74  usbblk->func->name, strerror ( rc ) );
75  goto err_reset;
76  }
77 
78  /* Open bulk OUT endpoint */
79  if ( ( rc = usb_endpoint_open ( &usbblk->out ) ) != 0 ) {
80  DBGC ( usbblk, "USBBLK %s could not open bulk OUT: %s\n",
81  usbblk->func->name, strerror ( rc ) );
82  goto err_open_out;
83  }
84 
85  /* Clear any bulk OUT halt condition */
86  if ( ( rc = usb_endpoint_clear_halt ( &usbblk->out ) ) != 0 ) {
87  DBGC ( usbblk, "USBBLK %s could not reset bulk OUT: %s\n",
88  usbblk->func->name, strerror ( rc ) );
89  goto err_clear_out;
90  }
91 
92  /* Open bulk IN endpoint */
93  if ( ( rc = usb_endpoint_open ( &usbblk->in ) ) != 0 ) {
94  DBGC ( usbblk, "USBBLK %s could not open bulk IN: %s\n",
95  usbblk->func->name, strerror ( rc ) );
96  goto err_open_in;
97  }
98 
99  /* Clear any bulk IN halt condition */
100  if ( ( rc = usb_endpoint_clear_halt ( &usbblk->in ) ) != 0 ) {
101  DBGC ( usbblk, "USBBLK %s could not reset bulk IN: %s\n",
102  usbblk->func->name, strerror ( rc ) );
103  goto err_clear_in;
104  }
105 
106  return 0;
107 
108  err_clear_in:
109  usb_endpoint_close ( &usbblk->in );
110  err_open_in:
111  err_clear_out:
112  usb_endpoint_close ( &usbblk->out );
113  err_open_out:
114  err_reset:
115  return rc;
116 }
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbblk.h:86
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Name.
Definition: usb.h:661
#define DBGC(...)
Definition: compiler.h:505
int usb_endpoint_clear_halt(struct usb_endpoint *ep)
Clear endpoint halt (if applicable)
Definition: usb.c:371
int usb_endpoint_open(struct usb_endpoint *ep)
Open USB endpoint.
Definition: usb.c:293
void usb_endpoint_close(struct usb_endpoint *ep)
Close USB endpoint.
Definition: usb.c:399
int open
Endpoint is open.
Definition: usb.h:404
int usb_control(struct usb_device *usb, unsigned int request, unsigned int value, unsigned int index, void *data, size_t len)
Issue USB control transaction.
Definition: usb.c:783
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
An object interface.
Definition: interface.h:124
#define USBBLK_RESET
Mass storage reset command.
Definition: usbblk.h:27
A USB device.
Definition: usb.h:708
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
struct usb_device * usb
USB device.
Definition: usb.h:663
struct usb_endpoint in
Bulk IN endpoint.
Definition: usbblk.h:88
struct usb_function * func
USB function.
Definition: usbblk.h:84
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), DBGC, usbblk_device::func, usbblk_device::in, usb_function::name, NULL, usb_endpoint::open, usbblk_device::out, rc, strerror(), usb_function::usb, usb_control(), usb_endpoint_clear_halt(), usb_endpoint_close(), usb_endpoint_open(), and USBBLK_RESET.

Referenced by usbblk_scsi_command().

◆ usbblk_close()

static void usbblk_close ( struct usbblk_device usbblk)
static

Close endpoints.

Parameters
usbblkUSB block device

Definition at line 123 of file usbblk.c.

123  {
124 
125  /* Close bulk OUT endpoint */
126  if ( usbblk->out.open )
127  usb_endpoint_close ( &usbblk->out );
128 
129  /* Close bulk IN endpoint */
130  if ( usbblk->in.open )
131  usb_endpoint_close ( &usbblk->in );
132 }
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbblk.h:86
void usb_endpoint_close(struct usb_endpoint *ep)
Close USB endpoint.
Definition: usb.c:399
int open
Endpoint is open.
Definition: usb.h:404
struct usb_endpoint in
Bulk IN endpoint.
Definition: usbblk.h:88

References usbblk_device::in, usb_endpoint::open, usbblk_device::out, and usb_endpoint_close().

Referenced by usbblk_scsi_close(), usbblk_scsi_command(), and usbblk_stop().

◆ usbblk_out_command()

static int usbblk_out_command ( struct usbblk_device usbblk)
static

Issue bulk OUT command.

Parameters
usbblkUSB block device
Return values
rcReturn status code

Definition at line 147 of file usbblk.c.

147  {
148  struct usbblk_command *cmd = &usbblk->cmd;
149  struct usbblk_command_wrapper *wrapper;
150  struct io_buffer *iobuf;
151  int rc;
152 
153  /* Sanity checks */
154  assert ( cmd->tag );
155  assert ( ! ( cmd->scsi.data_in_len && cmd->scsi.data_out_len ) );
156 
157  /* Allocate I/O buffer */
158  iobuf = alloc_iob ( sizeof ( *wrapper ) );
159  if ( ! iobuf ) {
160  rc = -ENOMEM;
161  goto err_alloc;
162  }
163 
164  /* Populate command */
165  wrapper = iob_put ( iobuf, sizeof ( *wrapper ) );
166  memset ( wrapper, 0, sizeof ( *wrapper ) );
168  wrapper->tag = cmd->tag; /* non-endian */
169  if ( cmd->scsi.data_out_len ) {
170  wrapper->len = cpu_to_le32 ( cmd->scsi.data_out_len );
171  } else {
172  wrapper->len = cpu_to_le32 ( cmd->scsi.data_in_len );
173  wrapper->flags = USB_DIR_IN;
174  }
175  wrapper->lun = ntohs ( cmd->scsi.lun.u16[0] );
176  wrapper->cblen = sizeof ( wrapper->cb );
177  memcpy ( wrapper->cb, &cmd->scsi.cdb, sizeof ( wrapper->cb ) );
178 
179  /* Issue command */
180  if ( ( rc = usb_stream ( &usbblk->out, iobuf, 0 ) ) != 0 ) {
181  DBGC ( usbblk, "USBBLK %s bulk OUT could not issue command: "
182  "%s\n", usbblk->func->name, strerror ( rc ) );
183  goto err_stream;
184  }
185 
186  return 0;
187 
188  err_stream:
189  free_iob ( iobuf );
190  err_alloc:
191  return rc;
192 }
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbblk.h:86
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:100
A USB mass storage command.
Definition: usbblk.h:67
#define iob_put(iobuf, len)
Definition: iobuf.h:120
uint8_t cb[16]
Command block.
Definition: usbblk.h:45
const char * name
Name.
Definition: usb.h:661
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:146
uint8_t cblen
Command block length.
Definition: usbblk.h:43
#define DBGC(...)
Definition: compiler.h:505
Command block wrapper.
Definition: usbblk.h:31
int usb_stream(struct usb_endpoint *ep, struct io_buffer *iobuf, int terminate)
Enqueue USB stream transfer.
Definition: usb.c:545
#define ntohs(value)
Definition: byteswap.h:136
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:129
uint32_t signature
Signature.
Definition: usbblk.h:33
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
uint8_t lun
LUN.
Definition: usbblk.h:41
#define USB_DIR_IN
Data transfer is from device to host.
Definition: usb.h:83
#define cpu_to_le32(value)
Definition: byteswap.h:107
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define USBBLK_COMMAND_SIGNATURE
Command block wrapper signature.
Definition: usbblk.h:49
struct usb_function * func
USB function.
Definition: usbblk.h:84
uint32_t tag
Tag.
Definition: usbblk.h:35
uint32_t len
Data transfer length.
Definition: usbblk.h:37
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
void * memset(void *dest, int character, size_t len) __nonnull
uint8_t flags
Flags.
Definition: usbblk.h:39
A persistent I/O buffer.
Definition: iobuf.h:33

References alloc_iob(), assert(), usbblk_command_wrapper::cb, usbblk_command_wrapper::cblen, cmd, usbblk_device::cmd, cpu_to_le32, DBGC, ENOMEM, usbblk_command_wrapper::flags, free_iob(), usbblk_device::func, iob_put, usbblk_command_wrapper::len, usbblk_command_wrapper::lun, memcpy(), memset(), usb_function::name, ntohs, usbblk_device::out, rc, usbblk_command_wrapper::signature, strerror(), usbblk_command_wrapper::tag, USB_DIR_IN, usb_stream(), and USBBLK_COMMAND_SIGNATURE.

Referenced by usbblk_start().

◆ usbblk_out_data()

static int usbblk_out_data ( struct usbblk_device usbblk)
static

Send bulk OUT data block.

Parameters
usbblkUSB block device
Return values
rcReturn status code

Definition at line 200 of file usbblk.c.

200  {
201  struct usbblk_command *cmd = &usbblk->cmd;
202  struct io_buffer *iobuf;
203  size_t len;
204  int rc;
205 
206  /* Calculate length */
207  assert ( cmd->tag );
208  assert ( cmd->scsi.data_out != UNULL );
209  assert ( cmd->offset < cmd->scsi.data_out_len );
210  len = ( cmd->scsi.data_out_len - cmd->offset );
211  if ( len > USBBLK_MAX_LEN )
213  assert ( ( len % usbblk->out.mtu ) == 0 );
214 
215  /* Allocate I/O buffer */
216  iobuf = alloc_iob ( len );
217  if ( ! iobuf ) {
218  rc = -ENOMEM;
219  goto err_alloc;
220  }
221 
222  /* Populate I/O buffer */
223  copy_from_user ( iob_put ( iobuf, len ), cmd->scsi.data_out,
224  cmd->offset, len );
225 
226  /* Send data */
227  if ( ( rc = usb_stream ( &usbblk->out, iobuf, 0 ) ) != 0 ) {
228  DBGC ( usbblk, "USBBLK %s bulk OUT could not send data: %s\n",
229  usbblk->func->name, strerror ( rc ) );
230  goto err_stream;
231  }
232 
233  /* Consume data */
234  cmd->offset += len;
235 
236  return 0;
237 
238  err_stream:
239  free_iob ( iobuf );
240  err_alloc:
241  return rc;
242 }
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbblk.h:86
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:100
A USB mass storage command.
Definition: usbblk.h:67
#define iob_put(iobuf, len)
Definition: iobuf.h:120
const char * name
Name.
Definition: usb.h:661
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:146
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
#define DBGC(...)
Definition: compiler.h:505
int usb_stream(struct usb_endpoint *ep, struct io_buffer *iobuf, int terminate)
Enqueue USB stream transfer.
Definition: usb.c:545
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:129
#define ENOMEM
Not enough space.
Definition: errno.h:534
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define USBBLK_MAX_LEN
Maximum length of USB data block.
Definition: usbblk.h:113
size_t mtu
Maximum transfer size.
Definition: usb.h:397
struct usb_function * func
USB function.
Definition: usbblk.h:84
#define UNULL
Equivalent of NULL for user pointers.
Definition: uaccess.h:36
uint32_t len
Length.
Definition: ena.h:14
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
A persistent I/O buffer.
Definition: iobuf.h:33

References alloc_iob(), assert(), cmd, usbblk_device::cmd, copy_from_user(), DBGC, ENOMEM, free_iob(), usbblk_device::func, iob_put, len, usb_endpoint::mtu, usb_function::name, usbblk_device::out, rc, strerror(), UNULL, usb_stream(), and USBBLK_MAX_LEN.

Referenced by usbblk_out_refill().

◆ usbblk_out_refill()

static int usbblk_out_refill ( struct usbblk_device usbblk)
static

Refill bulk OUT endpoint.

Parameters
usbblkUSB block device
Return values
rcReturn status code

Definition at line 250 of file usbblk.c.

250  {
251  struct usbblk_command *cmd = &usbblk->cmd;
252  int rc;
253 
254  /* Sanity checks */
255  assert ( cmd->tag );
256 
257  /* Refill endpoint */
258  while ( ( cmd->offset < cmd->scsi.data_out_len ) &&
259  ( usbblk->out.fill < USBBLK_MAX_FILL ) ) {
260  if ( ( rc = usbblk_out_data ( usbblk ) ) != 0 )
261  return rc;
262  }
263 
264  return 0;
265 }
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbblk.h:86
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:100
#define USBBLK_MAX_FILL
Maximum endpoint fill level.
Definition: usbblk.h:119
A USB mass storage command.
Definition: usbblk.h:67
static int usbblk_out_data(struct usbblk_device *usbblk)
Send bulk OUT data block.
Definition: usbblk.c:200
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
unsigned int fill
Buffer fill level.
Definition: usb.h:406
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29

References assert(), cmd, usbblk_device::cmd, usb_endpoint::fill, usbblk_device::out, rc, USBBLK_MAX_FILL, and usbblk_out_data().

Referenced by usbblk_step().

◆ usbblk_out_complete()

static void usbblk_out_complete ( struct usb_endpoint ep,
struct io_buffer iobuf,
int  rc 
)
static

Complete bulk OUT transfer.

Parameters
epUSB endpoint
iobufI/O buffer
rcCompletion status code

Definition at line 274 of file usbblk.c.

275  {
276  struct usbblk_device *usbblk =
277  container_of ( ep, struct usbblk_device, out );
278  struct usbblk_command *cmd = &usbblk->cmd;
279 
280  /* Ignore cancellations after closing endpoint */
281  if ( ! ep->open )
282  goto drop;
283 
284  /* Sanity check */
285  assert ( cmd->tag );
286 
287  /* Check for failures */
288  if ( rc != 0 ) {
289  DBGC ( usbblk, "USBBLK %s bulk OUT failed: %s\n",
290  usbblk->func->name, strerror ( rc ) );
291  goto err;
292  }
293 
294  /* Trigger refill process, if applicable */
295  if ( cmd->offset < cmd->scsi.data_out_len )
296  process_add ( &usbblk->process );
297 
298  drop:
299  /* Free I/O buffer */
300  free_iob ( iobuf );
301 
302  return;
303 
304  err:
305  free_iob ( iobuf );
306  usbblk_stop ( usbblk, rc );
307 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:100
struct process process
Command process.
Definition: usbblk.h:95
A USB mass storage command.
Definition: usbblk.h:67
const char * name
Name.
Definition: usb.h:661
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:146
#define DBGC(...)
Definition: compiler.h:505
int open
Endpoint is open.
Definition: usb.h:404
__be32 out[4]
Definition: CIB_PRM.h:36
static void usbblk_stop(struct usbblk_device *usbblk, int rc)
Stop SCSI command.
Definition: usbblk.c:552
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
A USB mass storage device.
Definition: usbblk.h:77
void process_add(struct process *process)
Add process to process list.
Definition: process.c:59
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
struct usb_function * func
USB function.
Definition: usbblk.h:84
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29

References assert(), cmd, usbblk_device::cmd, container_of, DBGC, free_iob(), usbblk_device::func, usb_function::name, usb_endpoint::open, out, usbblk_device::process, process_add(), rc, strerror(), and usbblk_stop().

◆ usbblk_in_data()

static int usbblk_in_data ( struct usbblk_device usbblk,
const void *  data,
size_t  len 
)
static

Handle bulk IN data block.

Parameters
usbblkUSB block device
dataData block
lenLength of data
Return values
rcReturn status code

Definition at line 329 of file usbblk.c.

330  {
331  struct usbblk_command *cmd = &usbblk->cmd;
332 
333  /* Sanity checks */
334  assert ( cmd->tag );
335  assert ( cmd->scsi.data_in != UNULL );
336  assert ( cmd->offset <= cmd->scsi.data_in_len );
337  assert ( len <= ( cmd->scsi.data_in_len - cmd->offset ) );
338 
339  /* Store data */
340  copy_to_user ( cmd->scsi.data_in, cmd->offset, data, len );
341  cmd->offset += len;
342 
343  return 0;
344 }
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:100
A USB mass storage command.
Definition: usbblk.h:67
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static __always_inline void copy_to_user(userptr_t dest, off_t dest_off, const void *src, size_t len)
Copy data to user buffer.
Definition: uaccess.h:324
#define UNULL
Equivalent of NULL for user pointers.
Definition: uaccess.h:36
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29

References assert(), cmd, usbblk_device::cmd, copy_to_user(), data, len, and UNULL.

Referenced by usbblk_in_complete().

◆ usbblk_in_status()

static int usbblk_in_status ( struct usbblk_device usbblk,
const void *  data,
size_t  len 
)
static

Handle bulk IN status.

Parameters
usbblkUSB block device
dataStatus data
lenLength of status data
Return values
rcReturn status code

Definition at line 354 of file usbblk.c.

355  {
356  struct usbblk_command *cmd = &usbblk->cmd;
357  const struct usbblk_status_wrapper *stat;
358 
359  /* Sanity checks */
360  assert ( cmd->tag );
361 
362  /* Validate length */
363  if ( len < sizeof ( *stat ) ) {
364  DBGC ( usbblk, "USBBLK %s bulk IN malformed status:\n",
365  usbblk->func->name );
366  DBGC_HDA ( usbblk, 0, data, len );
367  return -EIO;
368  }
369  stat = data;
370 
371  /* Validate signature */
372  if ( stat->signature != cpu_to_le32 ( USBBLK_STATUS_SIGNATURE ) ) {
373  DBGC ( usbblk, "USBBLK %s bulk IN invalid signature %08x:\n",
374  usbblk->func->name, le32_to_cpu ( stat->signature ) );
375  DBGC_HDA ( usbblk, 0, stat, sizeof ( *stat ) );
376  return -EIO;
377  }
378 
379  /* Validate tag */
380  if ( stat->tag != cmd->tag ) {
381  DBGC ( usbblk, "USBBLK %s bulk IN tag mismatch (got %08x, "
382  "expected %08x):\n",
383  usbblk->func->name, stat->tag, cmd->tag );
384  DBGC_HDA ( usbblk, 0, stat, sizeof ( *stat ) );
385  return -EIO;
386  }
387 
388  /* Check status */
389  if ( stat->status ) {
390  DBGC ( usbblk, "USBBLK %s bulk IN status %02x:\n",
391  usbblk->func->name, stat->status );
392  DBGC_HDA ( usbblk, 0, stat, sizeof ( *stat ) );
393  return -EIO;
394  }
395 
396  /* Check for residual data */
397  if ( stat->residue ) {
398  DBGC ( usbblk, "USBBLK %s bulk IN residue %#x:\n",
399  usbblk->func->name, le32_to_cpu ( stat->residue ) );
400  return -EIO;
401  }
402 
403  /* Mark command as complete */
404  usbblk_stop ( usbblk, 0 );
405 
406  return 0;
407 }
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:100
A USB mass storage command.
Definition: usbblk.h:67
const char * name
Name.
Definition: usb.h:661
#define le32_to_cpu(value)
Definition: byteswap.h:113
#define DBGC(...)
Definition: compiler.h:505
static void usbblk_stop(struct usbblk_device *usbblk, int rc)
Stop SCSI command.
Definition: usbblk.c:552
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define DBGC_HDA(...)
Definition: compiler.h:506
#define cpu_to_le32(value)
Definition: byteswap.h:107
#define USBBLK_STATUS_SIGNATURE
Command status wrapper signature.
Definition: usbblk.h:64
uint32_t residue
Data residue.
Definition: usbblk.h:58
Command status wrapper.
Definition: usbblk.h:52
struct usb_function * func
USB function.
Definition: usbblk.h:84
uint32_t tag
Tag.
Definition: usbblk.h:56
uint32_t len
Length.
Definition: ena.h:14
#define EIO
Input/output error.
Definition: errno.h:433
uint8_t status
Status.
Definition: usbblk.h:60
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint32_t signature
Signature.
Definition: usbblk.h:54
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29

References assert(), cmd, usbblk_device::cmd, cpu_to_le32, data, DBGC, DBGC_HDA, EIO, usbblk_device::func, le32_to_cpu, len, usb_function::name, usbblk_status_wrapper::residue, usbblk_status_wrapper::signature, usbblk_status_wrapper::status, usbblk_status_wrapper::tag, USBBLK_STATUS_SIGNATURE, and usbblk_stop().

Referenced by usbblk_in_complete().

◆ usbblk_in_refill()

static int usbblk_in_refill ( struct usbblk_device usbblk)
static

Refill bulk IN endpoint.

Parameters
usbblkUSB block device
Return values
rcReturn status code

Definition at line 415 of file usbblk.c.

415  {
416  struct usbblk_command *cmd = &usbblk->cmd;
417  struct usbblk_status_wrapper *stat;
418  size_t remaining;
419  unsigned int max;
420  int rc;
421 
422  /* Sanity checks */
423  assert ( cmd->tag );
424 
425  /* Calculate maximum required refill */
426  remaining = sizeof ( *stat );
427  if ( cmd->scsi.data_in_len ) {
428  assert ( cmd->offset <= cmd->scsi.data_in_len );
429  remaining += ( cmd->scsi.data_in_len - cmd->offset );
430  }
431  max = ( ( remaining + USBBLK_MAX_LEN - 1 ) / USBBLK_MAX_LEN );
432 
433  /* Refill bulk IN endpoint */
434  if ( ( rc = usb_refill_limit ( &usbblk->in, max ) ) != 0 )
435  return rc;
436 
437  return 0;
438 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:100
A USB mass storage command.
Definition: usbblk.h:67
#define max(x, y)
Definition: ath.h:39
int usb_refill_limit(struct usb_endpoint *ep, unsigned int max)
Refill endpoint up to specified limit.
Definition: usb.c:660
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define USBBLK_MAX_LEN
Maximum length of USB data block.
Definition: usbblk.h:113
struct usb_endpoint in
Bulk IN endpoint.
Definition: usbblk.h:88
Command status wrapper.
Definition: usbblk.h:52
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29

References assert(), cmd, usbblk_device::cmd, usbblk_device::in, max, rc, usb_refill_limit(), and USBBLK_MAX_LEN.

Referenced by usbblk_step().

◆ usbblk_in_complete()

static void usbblk_in_complete ( struct usb_endpoint ep,
struct io_buffer iobuf,
int  rc 
)
static

Complete bulk IN transfer.

Parameters
epUSB endpoint
iobufI/O buffer
rcCompletion status code

Definition at line 447 of file usbblk.c.

448  {
449  struct usbblk_device *usbblk =
450  container_of ( ep, struct usbblk_device, in );
451  struct usbblk_command *cmd = &usbblk->cmd;
452  size_t remaining;
453  size_t len;
454 
455  /* Ignore cancellations after closing endpoint */
456  if ( ! ep->open )
457  goto drop;
458 
459  /* Sanity check */
460  assert ( cmd->tag );
461 
462  /* Handle errors */
463  if ( rc != 0 ) {
464  DBGC ( usbblk, "USBBLK %s bulk IN failed: %s\n",
465  usbblk->func->name, strerror ( rc ) );
466  goto err;
467  }
468 
469  /* Trigger refill process */
470  process_add ( &usbblk->process );
471 
472  /* Handle data portion, if any */
473  if ( cmd->scsi.data_in_len ) {
474  assert ( cmd->offset <= cmd->scsi.data_in_len );
475  remaining = ( cmd->scsi.data_in_len - cmd->offset );
476  len = iob_len ( iobuf );
477  if ( len > remaining )
478  len = remaining;
479  if ( len ) {
480  if ( ( rc = usbblk_in_data ( usbblk, iobuf->data,
481  len ) ) != 0 )
482  goto err;
483  iob_pull ( iobuf, len );
484  }
485  }
486 
487  /* Handle status portion, if any */
488  len = iob_len ( iobuf );
489  if ( len ) {
490  if ( ( rc = usbblk_in_status ( usbblk, iobuf->data,
491  len ) ) != 0 )
492  goto err;
493  }
494 
495  drop:
496  /* Free I/O buffer */
497  free_iob ( iobuf );
498 
499  return;
500 
501  err:
502  free_iob ( iobuf );
503  usbblk_stop ( usbblk, rc );
504 }
#define iob_pull(iobuf, len)
Definition: iobuf.h:102
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:100
struct process process
Command process.
Definition: usbblk.h:95
A USB mass storage command.
Definition: usbblk.h:67
static int usbblk_in_data(struct usbblk_device *usbblk, const void *data, size_t len)
Handle bulk IN data block.
Definition: usbblk.c:329
const char * name
Name.
Definition: usb.h:661
__be32 in[4]
Definition: CIB_PRM.h:35
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:146
#define DBGC(...)
Definition: compiler.h:505
int open
Endpoint is open.
Definition: usb.h:404
static void usbblk_stop(struct usbblk_device *usbblk, int rc)
Stop SCSI command.
Definition: usbblk.c:552
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
A USB mass storage device.
Definition: usbblk.h:77
void process_add(struct process *process)
Add process to process list.
Definition: process.c:59
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:155
struct usb_function * func
USB function.
Definition: usbblk.h:84
uint32_t len
Length.
Definition: ena.h:14
void * data
Start of data.
Definition: iobuf.h:48
static int usbblk_in_status(struct usbblk_device *usbblk, const void *data, size_t len)
Handle bulk IN status.
Definition: usbblk.c:354
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29

References assert(), cmd, usbblk_device::cmd, container_of, io_buffer::data, DBGC, free_iob(), usbblk_device::func, in, iob_len(), iob_pull, len, usb_function::name, usb_endpoint::open, usbblk_device::process, process_add(), rc, strerror(), usbblk_in_data(), usbblk_in_status(), and usbblk_stop().

◆ usbblk_step()

static void usbblk_step ( struct usbblk_device usbblk)
static

Refill endpoints.

Parameters
usbblkUSB block device

Definition at line 523 of file usbblk.c.

523  {
524 
525  /* Refill bulk OUT endpoint */
526  usbblk_out_refill ( usbblk );
527 
528  /* Refill bulk IN endpoint */
529  usbblk_in_refill ( usbblk );
530 }
static int usbblk_in_refill(struct usbblk_device *usbblk)
Refill bulk IN endpoint.
Definition: usbblk.c:415
static int usbblk_out_refill(struct usbblk_device *usbblk)
Refill bulk OUT endpoint.
Definition: usbblk.c:250

References usbblk_in_refill(), and usbblk_out_refill().

◆ usbblk_start()

static int usbblk_start ( struct usbblk_device usbblk,
struct scsi_cmd scsicmd 
)
static

Start new SCSI command.

Parameters
usbblkUSB block device
scsicmdSCSI command
Return values
rcReturn status code

Definition at line 578 of file usbblk.c.

579  {
580  struct usbblk_command *cmd = &usbblk->cmd;
581  int rc;
582 
583  /* Fail if command is in progress */
584  if ( cmd->tag ) {
585  rc = -EBUSY;
586  DBGC ( usbblk, "USBBLK %s cannot support multiple commands\n",
587  usbblk->func->name );
588  goto err_busy;
589  }
590 
591  /* Refuse bidirectional commands */
592  if ( scsicmd->data_in_len && scsicmd->data_out_len ) {
593  rc = -EOPNOTSUPP;
594  DBGC ( usbblk, "USBBLK %s cannot support bidirectional "
595  "commands\n", usbblk->func->name );
596  goto err_bidirectional;
597  }
598 
599  /* Sanity checks */
600  assert ( ! process_running ( &usbblk->process ) );
601  assert ( cmd->offset == 0 );
602 
603  /* Initialise command */
604  memcpy ( &cmd->scsi, scsicmd, sizeof ( cmd->scsi ) );
605  cmd->tag = ( USBBLK_TAG_MAGIC | ++usbblk_tag );
606 
607  /* Issue bulk OUT command */
608  if ( ( rc = usbblk_out_command ( usbblk ) ) != 0 )
609  goto err_command;
610 
611  /* Start refill process */
612  process_add ( &usbblk->process );
613 
614  return 0;
615 
616  err_command:
617  memset ( &usbblk->cmd, 0, sizeof ( usbblk->cmd ) );
618  err_bidirectional:
619  err_busy:
620  return rc;
621 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:100
struct process process
Command process.
Definition: usbblk.h:95
A USB mass storage command.
Definition: usbblk.h:67
size_t data_out_len
Data-out buffer length.
Definition: scsi.h:260
#define EBUSY
Device or resource busy.
Definition: errno.h:338
const char * name
Name.
Definition: usb.h:661
size_t data_in_len
Data-in buffer length.
Definition: scsi.h:267
#define DBGC(...)
Definition: compiler.h:505
static uint16_t usbblk_tag
Next command tag.
Definition: usbblk.c:544
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
void process_add(struct process *process)
Add process to process list.
Definition: process.c:59
#define EOPNOTSUPP
Operation not supported on socket.
Definition: errno.h:604
static int process_running(struct process *process)
Check if process is running.
Definition: process.h:175
struct usb_function * func
USB function.
Definition: usbblk.h:84
#define USBBLK_TAG_MAGIC
Command tag magic.
Definition: usbblk.h:107
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
static int usbblk_out_command(struct usbblk_device *usbblk)
Issue bulk OUT command.
Definition: usbblk.c:147
void * memset(void *dest, int character, size_t len) __nonnull

References assert(), cmd, usbblk_device::cmd, scsi_cmd::data_in_len, scsi_cmd::data_out_len, DBGC, EBUSY, EOPNOTSUPP, usbblk_device::func, memcpy(), memset(), usb_function::name, usbblk_device::process, process_add(), process_running(), rc, usbblk_out_command(), usbblk_tag, and USBBLK_TAG_MAGIC.

Referenced by usbblk_scsi_command().

◆ usbblk_scsi_window()

static size_t usbblk_scsi_window ( struct usbblk_device usbblk)
static

Check SCSI command flow-control window.

Parameters
usbblkUSB block device
Return values
lenLength of window

Definition at line 645 of file usbblk.c.

645  {
646  struct usbblk_command *cmd = &usbblk->cmd;
647 
648  /* Allow a single command if no command is currently in progress */
649  return ( cmd->tag ? 0 : 1 );
650 }
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:100
A USB mass storage command.
Definition: usbblk.h:67
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29

References cmd, and usbblk_device::cmd.

◆ usbblk_scsi_command()

static int usbblk_scsi_command ( struct usbblk_device usbblk,
struct interface data,
struct scsi_cmd scsicmd 
)
static

Issue SCSI command.

Parameters
usbblkUSB block device
dataSCSI data interface
scsicmdSCSI command
Return values
tagCommand tag, or negative error

Definition at line 660 of file usbblk.c.

662  {
663  struct usbblk_command *cmd = &usbblk->cmd;
664  int rc;
665 
666  /* (Re)open endpoints if needed */
667  if ( ( ! usbblk->in.open ) && ( ( rc = usbblk_open ( usbblk ) ) != 0 ) )
668  goto err_open;
669 
670  /* Start new command */
671  if ( ( rc = usbblk_start ( usbblk, scsicmd ) ) != 0 )
672  goto err_start;
673 
674  /* Attach to parent interface and return */
675  intf_plug_plug ( &usbblk->data, data );
676  return cmd->tag;
677 
678  usbblk_stop ( usbblk, rc );
679  err_start:
680  usbblk_close ( usbblk );
681  err_open:
682  return rc;
683 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:100
A USB mass storage command.
Definition: usbblk.h:67
static int usbblk_start(struct usbblk_device *usbblk, struct scsi_cmd *scsicmd)
Start new SCSI command.
Definition: usbblk.c:578
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:107
int open
Endpoint is open.
Definition: usb.h:404
static void usbblk_stop(struct usbblk_device *usbblk, int rc)
Stop SCSI command.
Definition: usbblk.c:552
static int usbblk_open(struct usbblk_device *usbblk)
Open endpoints.
Definition: usbblk.c:61
struct usb_endpoint in
Bulk IN endpoint.
Definition: usbblk.h:88
uint8_t data[48]
Additional event data.
Definition: ena.h:22
static void usbblk_close(struct usbblk_device *usbblk)
Close endpoints.
Definition: usbblk.c:123
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
struct interface data
SCSI data interface.
Definition: usbblk.h:93

References cmd, usbblk_device::cmd, data, usbblk_device::data, usbblk_device::in, intf_plug_plug(), usb_endpoint::open, rc, usbblk_close(), usbblk_open(), usbblk_start(), and usbblk_stop().

◆ usbblk_scsi_close()

static void usbblk_scsi_close ( struct usbblk_device usbblk,
int  rc 
)
static

Close SCSI interface.

Parameters
usbblkUSB block device
rcReason for close

Definition at line 691 of file usbblk.c.

691  {
692 
693  /* Restart interfaces */
694  intfs_restart ( rc, &usbblk->scsi, &usbblk->data, NULL );
695 
696  /* Stop any in-progress command */
697  usbblk_stop ( usbblk, rc );
698 
699  /* Close endpoints */
700  usbblk_close ( usbblk );
701 
702  /* Flag as closed */
703  usbblk->opened = 0;
704 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intfs_restart(int rc,...)
Shut down and restart multiple object interfaces.
Definition: interface.c:386
static void usbblk_stop(struct usbblk_device *usbblk, int rc)
Stop SCSI command.
Definition: usbblk.c:552
struct interface scsi
SCSI command-issuing interface.
Definition: usbblk.h:91
int opened
Device opened flag.
Definition: usbblk.h:97
static void usbblk_close(struct usbblk_device *usbblk)
Close endpoints.
Definition: usbblk.c:123
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
struct interface data
SCSI data interface.
Definition: usbblk.h:93

References usbblk_device::data, intfs_restart(), NULL, usbblk_device::opened, rc, usbblk_device::scsi, usbblk_close(), and usbblk_stop().

Referenced by usbblk_remove().

◆ usbblk_efi_describe()

static EFI_DEVICE_PATH_PROTOCOL* usbblk_efi_describe ( struct usbblk_device usbblk)
static

Describe as an EFI device path.

Parameters
usbblkUSB block device
Return values
pathEFI device path, or NULL on error

Definition at line 713 of file usbblk.c.

713  {
714 
715  return efi_usb_path ( usbblk->func );
716 }
EFI_DEVICE_PATH_PROTOCOL * efi_usb_path(struct usb_function *func)
Construct EFI device path for USB function.
Definition: efi_path.c:626
struct usb_function * func
USB function.
Definition: usbblk.h:84

References efi_usb_path(), and usbblk_device::func.

◆ usbblk_find()

static struct usbblk_device* usbblk_find ( const char *  name)
static

Find USB block device.

Parameters
nameUSB block device name
Return values
usbblkUSB block device, or NULL

Definition at line 744 of file usbblk.c.

744  {
745  struct usbblk_device *usbblk;
746 
747  /* Look for matching device */
748  list_for_each_entry ( usbblk, &usbblk_devices, list ) {
749  if ( strcmp ( usbblk->func->name, name ) == 0 )
750  return usbblk;
751  }
752 
753  return NULL;
754 }
const char * name
Definition: ath9k_hw.c:1984
const char * name
Name.
Definition: usb.h:661
A USB mass storage device.
Definition: usbblk.h:77
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
struct list_head list
List of devices.
Definition: usbblk.h:81
struct usb_function * func
USB function.
Definition: usbblk.h:84
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References usbblk_device::func, usbblk_device::list, list_for_each_entry, usb_function::name, name, NULL, and strcmp().

Referenced by usbblk_open_uri().

◆ usbblk_open_uri()

static int usbblk_open_uri ( struct interface parent,
struct uri uri 
)
static

Open USB block device URI.

Parameters
parentParent interface
uriURI
Return values
rcReturn status code

Definition at line 763 of file usbblk.c.

763  {
764  static struct scsi_lun lun;
765  struct usbblk_device *usbblk;
766  int rc;
767 
768  /* Sanity check */
769  if ( ! uri->opaque )
770  return -EINVAL;
771 
772  /* Find matching device */
773  usbblk = usbblk_find ( uri->opaque );
774  if ( ! usbblk )
775  return -ENOENT;
776 
777  /* Fail if device is already open */
778  if ( usbblk->opened )
779  return -EBUSY;
780 
781  /* Open SCSI device */
782  if ( ( rc = scsi_open ( parent, &usbblk->scsi, &lun ) ) != 0 ) {
783  DBGC ( usbblk, "USBBLK %s could not open SCSI device: %s\n",
784  usbblk->func->name, strerror ( rc ) );
785  return rc;
786  }
787 
788  /* Mark as opened */
789  usbblk->opened = 1;
790 
791  return 0;
792 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EBUSY
Device or resource busy.
Definition: errno.h:338
const char * name
Name.
Definition: usb.h:661
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
int scsi_open(struct interface *block, struct interface *scsi, struct scsi_lun *lun)
Open SCSI device.
Definition: scsi.c:984
struct interface scsi
SCSI command-issuing interface.
Definition: usbblk.h:91
A USB mass storage device.
Definition: usbblk.h:77
uint8_t lun
Logical Unit Number.
Definition: edd.h:32
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
struct usb_function * func
USB function.
Definition: usbblk.h:84
const char * opaque
Opaque part.
Definition: uri.h:70
static struct usbblk_device * usbblk_find(const char *name)
Find USB block device.
Definition: usbblk.c:744
int opened
Device opened flag.
Definition: usbblk.h:97
A Uniform Resource Identifier.
Definition: uri.h:64
A SCSI LUN.
Definition: scsi.h:236

References DBGC, EBUSY, EINVAL, ENOENT, usbblk_device::func, lun, usb_function::name, uri::opaque, usbblk_device::opened, rc, usbblk_device::scsi, scsi_open(), strerror(), and usbblk_find().

◆ usbblk_probe()

static int usbblk_probe ( struct usb_function func,
struct usb_configuration_descriptor config 
)
static

Probe device.

Parameters
funcUSB function
configConfiguration descriptor
Return values
rcReturn status code

Definition at line 814 of file usbblk.c.

815  {
816  struct usb_device *usb = func->usb;
817  struct usbblk_device *usbblk;
819  int rc;
820 
821  /* Allocate and initialise structure */
822  usbblk = zalloc ( sizeof ( *usbblk ) );
823  if ( ! usbblk ) {
824  rc = -ENOMEM;
825  goto err_alloc;
826  }
827  usbblk->func = func;
828  usb_endpoint_init ( &usbblk->out, usb, &usbblk_out_operations );
829  usb_endpoint_init ( &usbblk->in, usb, &usbblk_in_operations );
831  intf_init ( &usbblk->scsi, &usbblk_scsi_desc, &usbblk->refcnt );
832  intf_init ( &usbblk->data, &usbblk_data_desc, &usbblk->refcnt );
834  &usbblk->refcnt );
835 
836  /* Locate interface descriptor */
837  desc = usb_interface_descriptor ( config, func->interface[0], 0 );
838  if ( ! desc ) {
839  DBGC ( usbblk, "USBBLK %s missing interface descriptor\n",
840  usbblk->func->name );
841  rc = -ENOENT;
842  goto err_desc;
843  }
844 
845  /* Describe endpoints */
846  if ( ( rc = usb_endpoint_described ( &usbblk->out, config, desc,
847  USB_BULK_OUT, 0 ) ) != 0 ) {
848  DBGC ( usbblk, "USBBLK %s could not describe bulk OUT: %s\n",
849  usbblk->func->name, strerror ( rc ) );
850  goto err_out;
851  }
852  if ( ( rc = usb_endpoint_described ( &usbblk->in, config, desc,
853  USB_BULK_IN, 0 ) ) != 0 ) {
854  DBGC ( usbblk, "USBBLK %s could not describe bulk IN: %s\n",
855  usbblk->func->name, strerror ( rc ) );
856  goto err_in;
857  }
858 
859  /* Add to list of devices */
860  list_add_tail ( &usbblk->list, &usbblk_devices );
861 
862  usb_func_set_drvdata ( func, usbblk );
863  return 0;
864 
865  err_in:
866  err_out:
867  err_desc:
868  ref_put ( &usbblk->refcnt );
869  err_alloc:
870  return rc;
871 }
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbblk.h:86
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define USBBLK_MAX_FILL
Maximum endpoint fill level.
Definition: usbblk.h:119
struct process process
Command process.
Definition: usbblk.h:95
uint8_t interface[0]
List of interface numbers.
Definition: usb.h:682
const char * name
Name.
Definition: usb.h:661
uint64_t desc
Microcode descriptor list physical address.
Definition: ucode.h:12
#define DBGC(...)
Definition: compiler.h:505
static struct usb_endpoint_driver_operations usbblk_in_operations
Bulk IN endpoint operations.
Definition: usbblk.c:507
#define ENOENT
No such file or directory.
Definition: errno.h:514
int usb_endpoint_described(struct usb_endpoint *ep, struct usb_configuration_descriptor *config, struct usb_interface_descriptor *interface, unsigned int type, unsigned int index)
Describe USB endpoint from device configuration.
Definition: usb.c:241
#define ENOMEM
Not enough space.
Definition: errno.h:534
A USB interface descriptor.
Definition: usb.h:230
struct refcnt refcnt
Reference count.
Definition: usbblk.h:79
struct interface scsi
SCSI command-issuing interface.
Definition: usbblk.h:91
#define USB_BULK_OUT
Bulk OUT endpoint (internal) type.
Definition: usb.h:281
A USB mass storage device.
Definition: usbblk.h:77
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
static void usb_refill_init(struct usb_endpoint *ep, size_t reserve, size_t len, unsigned int max)
Initialise USB endpoint refill.
Definition: usb.h:602
static void usb_func_set_drvdata(struct usb_function *func, void *priv)
Set USB function driver private data.
Definition: usb.h:692
struct list_head list
List of devices.
Definition: usbblk.h:81
A USB device.
Definition: usb.h:708
struct usb_interface_descriptor * usb_interface_descriptor(struct usb_configuration_descriptor *config, unsigned int interface, unsigned int alternate)
Locate USB interface descriptor.
Definition: usb.c:143
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
#define USBBLK_MAX_LEN
Maximum length of USB data block.
Definition: usbblk.h:113
static void process_init_stopped(struct process *process, struct process_descriptor *desc, struct refcnt *refcnt)
Initialise process without adding to process list.
Definition: process.h:145
struct usb_device * usb
USB device.
Definition: usb.h:663
static void usb_endpoint_init(struct usb_endpoint *ep, struct usb_device *usb, struct usb_endpoint_driver_operations *driver)
Initialise USB endpoint.
Definition: usb.h:525
struct usb_endpoint in
Bulk IN endpoint.
Definition: usbblk.h:88
static struct interface_descriptor usbblk_data_desc
SCSI data interface descriptor.
Definition: usbblk.c:636
#define USB_BULK_IN
Bulk IN endpoint (internal) type.
Definition: usb.h:284
struct usb_function * func
USB function.
Definition: usbblk.h:84
static struct interface_descriptor usbblk_scsi_desc
SCSI command interface descriptor.
Definition: usbblk.c:728
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:203
static struct process_descriptor usbblk_process_desc
Refill process descriptor.
Definition: usbblk.c:533
static struct usb_endpoint_driver_operations usbblk_out_operations
Bulk OUT endpoint operations.
Definition: usbblk.c:310
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
struct interface data
SCSI data interface.
Definition: usbblk.h:93

References usbblk_device::data, DBGC, desc, ENOENT, ENOMEM, usbblk_device::func, usbblk_device::in, usb_function::interface, intf_init(), usbblk_device::list, list_add_tail, usb_function::name, usbblk_device::out, usbblk_device::process, process_init_stopped(), rc, ref_put, usbblk_device::refcnt, usbblk_device::scsi, strerror(), usb_function::usb, USB_BULK_IN, USB_BULK_OUT, usb_endpoint_described(), usb_endpoint_init(), usb_func_set_drvdata(), usb_interface_descriptor(), usb_refill_init(), usbblk_data_desc, usbblk_in_operations, USBBLK_MAX_FILL, USBBLK_MAX_LEN, usbblk_out_operations, usbblk_process_desc, usbblk_scsi_desc, and zalloc().

◆ usbblk_remove()

static void usbblk_remove ( struct usb_function func)
static

Remove device.

Parameters
funcUSB function

Definition at line 878 of file usbblk.c.

878  {
879  struct usbblk_device *usbblk = usb_func_get_drvdata ( func );
880 
881  /* Remove from list of devices */
882  list_del ( &usbblk->list );
883 
884  /* Close all interfaces */
885  usbblk_scsi_close ( usbblk, -ENODEV );
886 
887  /* Shut down interfaces */
888  intfs_shutdown ( -ENODEV, &usbblk->scsi, &usbblk->data, NULL );
889 
890  /* Drop reference */
891  ref_put ( &usbblk->refcnt );
892 }
static void * usb_func_get_drvdata(struct usb_function *func)
Get USB function driver private data.
Definition: usb.h:703
void intfs_shutdown(int rc,...)
Shut down multiple object interfaces.
Definition: interface.c:326
static void usbblk_scsi_close(struct usbblk_device *usbblk, int rc)
Close SCSI interface.
Definition: usbblk.c:691
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
struct refcnt refcnt
Reference count.
Definition: usbblk.h:79
struct interface scsi
SCSI command-issuing interface.
Definition: usbblk.h:91
A USB mass storage device.
Definition: usbblk.h:77
struct list_head list
List of devices.
Definition: usbblk.h:81
#define ENODEV
No such device.
Definition: errno.h:509
struct usb_function * func
USB function.
Definition: usbblk.h:84
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
struct interface data
SCSI data interface.
Definition: usbblk.h:93

References usbblk_device::data, ENODEV, usbblk_device::func, intfs_shutdown(), usbblk_device::list, list_del, NULL, ref_put, usbblk_device::refcnt, usbblk_device::scsi, usb_func_get_drvdata(), and usbblk_scsi_close().

Variable Documentation

◆ usbblk_out_operations

struct usb_endpoint_driver_operations usbblk_out_operations
static
Initial value:
= {
.complete = usbblk_out_complete,
}
static void usbblk_out_complete(struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
Complete bulk OUT transfer.
Definition: usbblk.c:274

Bulk OUT endpoint operations.

Definition at line 310 of file usbblk.c.

Referenced by usbblk_probe().

◆ usbblk_in_operations

struct usb_endpoint_driver_operations usbblk_in_operations
static
Initial value:
= {
.complete = usbblk_in_complete,
}
static void usbblk_in_complete(struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
Complete bulk IN transfer.
Definition: usbblk.c:447

Bulk IN endpoint operations.

Definition at line 507 of file usbblk.c.

Referenced by usbblk_probe().

◆ usbblk_process_desc

struct process_descriptor usbblk_process_desc
static
Initial value:
=
A process.
Definition: process.h:17
A USB mass storage device.
Definition: usbblk.h:77
#define PROC_DESC(object_type, process, _step)
Define a process descriptor.
Definition: process.h:82
static void usbblk_step(struct usbblk_device *usbblk)
Refill endpoints.
Definition: usbblk.c:523

Refill process descriptor.

Definition at line 533 of file usbblk.c.

Referenced by usbblk_probe().

◆ usbblk_tag

uint16_t usbblk_tag
static

Next command tag.

Definition at line 544 of file usbblk.c.

Referenced by usbblk_start().

◆ usbblk_data_operations

struct interface_operation usbblk_data_operations[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:249
static void usbblk_stop(struct usbblk_device *usbblk, int rc)
Stop SCSI command.
Definition: usbblk.c:552
A USB mass storage device.
Definition: usbblk.h:77
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32

SCSI data interface operations.

Definition at line 631 of file usbblk.c.

◆ usbblk_data_desc

struct interface_descriptor usbblk_data_desc
static
Initial value:
=
A USB mass storage device.
Definition: usbblk.h:77
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:80
static struct interface_operation usbblk_data_operations[]
SCSI data interface operations.
Definition: usbblk.c:631
uint8_t data[48]
Additional event data.
Definition: ena.h:22

SCSI data interface descriptor.

Definition at line 636 of file usbblk.c.

Referenced by usbblk_probe().

◆ usbblk_scsi_operations

struct interface_operation usbblk_scsi_operations[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:249
size_t xfer_window(struct interface *intf)
Check flow control window.
Definition: xfer.c:116
static void usbblk_scsi_close(struct usbblk_device *usbblk, int rc)
Close SCSI interface.
Definition: usbblk.c:691
A SCSI command.
Definition: scsi.c:262
A USB mass storage device.
Definition: usbblk.h:77
static int usbblk_scsi_command(struct usbblk_device *usbblk, struct interface *data, struct scsi_cmd *scsicmd)
Issue SCSI command.
Definition: usbblk.c:660
#define EFI_INTF_OP
Definition: efi.h:350
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
static size_t usbblk_scsi_window(struct usbblk_device *usbblk)
Check SCSI command flow-control window.
Definition: usbblk.c:645
EFI_DEVICE_PATH_PROTOCOL * efi_describe(struct interface *intf)
Describe object as an EFI device path.
Definition: efi_path.c:680
static EFI_DEVICE_PATH_PROTOCOL * usbblk_efi_describe(struct usbblk_device *usbblk)
Describe as an EFI device path.
Definition: usbblk.c:713

SCSI command interface operations.

Definition at line 719 of file usbblk.c.

◆ usbblk_scsi_desc

struct interface_descriptor usbblk_scsi_desc
static
Initial value:
=
struct interface scsi
SCSI command-issuing interface.
Definition: usbblk.h:91
A USB mass storage device.
Definition: usbblk.h:77
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:80
static struct interface_operation usbblk_scsi_operations[]
SCSI command interface operations.
Definition: usbblk.c:719

SCSI command interface descriptor.

Definition at line 728 of file usbblk.c.

Referenced by usbblk_probe().

◆ __uri_opener

struct uri_opener usbblk_uri_opener __uri_opener
Initial value:
= {
.scheme = "usb",
.open = usbblk_open_uri,
}
static int usbblk_open_uri(struct interface *parent, struct uri *uri)
Open USB block device URI.
Definition: usbblk.c:763

USB block device URI opener.

Definition at line 795 of file usbblk.c.

◆ usbblk_ids

struct usb_device_id usbblk_ids[]
static
Initial value:
= {
{
.name = "usbblk",
.vendor = USB_ANY_ID,
.product = USB_ANY_ID,
},
}
#define USB_ANY_ID
Match-anything ID.
Definition: usb.h:1347

Mass storage class device IDs.

Definition at line 895 of file usbblk.c.

◆ __usb_driver

struct usb_driver usbblk_driver __usb_driver
Initial value:
= {
.ids = usbblk_ids,
.id_count = ( sizeof ( usbblk_ids ) / sizeof ( usbblk_ids[0] ) ),
.score = USB_SCORE_NORMAL,
.probe = usbblk_probe,
.remove = usbblk_remove,
}
#define USB_PROTOCOL_MSC_BULK
Bulk-only transport protocol.
Definition: usbblk.h:24
#define USB_CLASS_ID(base, subclass, protocol)
Construct USB class ID.
Definition: usb.h:1363
Normal driver.
Definition: usb.h:1427
static void usbblk_remove(struct usb_function *func)
Remove device.
Definition: usbblk.c:878
#define USB_SUBCLASS_MSC_SCSI
SCSI command set subclass code.
Definition: usbblk.h:21
static int usbblk_probe(struct usb_function *func, struct usb_configuration_descriptor *config)
Probe device.
Definition: usbblk.c:814
#define USB_CLASS_MSC
Mass storage class code.
Definition: usbblk.h:18
static struct usb_device_id usbblk_ids[]
Mass storage class device IDs.
Definition: usbblk.c:895

Mass storage driver.

Definition at line 904 of file usbblk.c.