iPXE
Functions | Variables
usbblk.c File Reference

USB mass storage driver. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.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)
 
 FILE_SECBOOT (PERMITTED)
 
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  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ 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 554 of file usbblk.c.

554  {
555 
556  /* Stop process */
557  process_del ( &usbblk->process );
558 
559  /* Reset command */
560  memset ( &usbblk->cmd, 0, sizeof ( usbblk->cmd ) );
561 
562  /* Close endpoints if an error occurred */
563  if ( rc != 0 ) {
564  DBGC ( usbblk, "USBBLK %s closing for error recovery\n",
565  usbblk->func->name );
566  usbblk_close ( usbblk );
567  }
568 
569  /* Terminate command */
570  intf_restart ( &usbblk->data, rc );
571 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:101
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:344
struct process process
Command process.
Definition: usbblk.h:96
const char * name
Name.
Definition: usb.h:676
#define DBGC(...)
Definition: compiler.h:505
void process_del(struct process *process)
Remove process from process list.
Definition: process.c:80
struct usb_function * func
USB function.
Definition: usbblk.h:85
static void usbblk_close(struct usbblk_device *usbblk)
Close endpoints.
Definition: usbblk.c:125
struct interface data
SCSI data interface.
Definition: usbblk.h:94
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 63 of file usbblk.c.

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

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 125 of file usbblk.c.

125  {
126 
127  /* Close bulk OUT endpoint */
128  if ( usbblk->out.open )
129  usb_endpoint_close ( &usbblk->out );
130 
131  /* Close bulk IN endpoint */
132  if ( usbblk->in.open )
133  usb_endpoint_close ( &usbblk->in );
134 }
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbblk.h:87
void usb_endpoint_close(struct usb_endpoint *ep)
Close USB endpoint.
Definition: usb.c:400
int open
Endpoint is open.
Definition: usb.h:419
struct usb_endpoint in
Bulk IN endpoint.
Definition: usbblk.h:89

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 149 of file usbblk.c.

149  {
150  struct usbblk_command *cmd = &usbblk->cmd;
151  struct usbblk_command_wrapper *wrapper;
152  struct io_buffer *iobuf;
153  int rc;
154 
155  /* Sanity checks */
156  assert ( cmd->tag );
157  assert ( ! ( cmd->scsi.data_in_len && cmd->scsi.data_out_len ) );
158 
159  /* Allocate I/O buffer */
160  iobuf = alloc_iob ( sizeof ( *wrapper ) );
161  if ( ! iobuf ) {
162  rc = -ENOMEM;
163  goto err_alloc;
164  }
165 
166  /* Populate command */
167  wrapper = iob_put ( iobuf, sizeof ( *wrapper ) );
168  memset ( wrapper, 0, sizeof ( *wrapper ) );
170  wrapper->tag = cmd->tag; /* non-endian */
171  if ( cmd->scsi.data_out_len ) {
172  wrapper->len = cpu_to_le32 ( cmd->scsi.data_out_len );
173  } else {
174  wrapper->len = cpu_to_le32 ( cmd->scsi.data_in_len );
175  wrapper->flags = USB_DIR_IN;
176  }
177  wrapper->lun = ntohs ( cmd->scsi.lun.u16[0] );
178  wrapper->cblen = sizeof ( wrapper->cb );
179  memcpy ( wrapper->cb, &cmd->scsi.cdb, sizeof ( wrapper->cb ) );
180 
181  /* Issue command */
182  if ( ( rc = usb_stream ( &usbblk->out, iobuf, 0 ) ) != 0 ) {
183  DBGC ( usbblk, "USBBLK %s bulk OUT could not issue command: "
184  "%s\n", usbblk->func->name, strerror ( rc ) );
185  goto err_stream;
186  }
187 
188  return 0;
189 
190  err_stream:
191  free_iob ( iobuf );
192  err_alloc:
193  return rc;
194 }
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbblk.h:87
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:101
A USB mass storage command.
Definition: usbblk.h:68
#define iob_put(iobuf, len)
Definition: iobuf.h:125
uint8_t cb[16]
Command block.
Definition: usbblk.h:46
const char * name
Name.
Definition: usb.h:676
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:153
uint8_t cblen
Command block length.
Definition: usbblk.h:44
#define DBGC(...)
Definition: compiler.h:505
Command block wrapper.
Definition: usbblk.h:32
int usb_stream(struct usb_endpoint *ep, struct io_buffer *iobuf, int terminate)
Enqueue USB stream transfer.
Definition: usb.c:546
#define ntohs(value)
Definition: byteswap.h:137
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:131
uint32_t signature
Signature.
Definition: usbblk.h:34
#define ENOMEM
Not enough space.
Definition: errno.h:535
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:42
#define USB_DIR_IN
Data transfer is from device to host.
Definition: usb.h:98
#define cpu_to_le32(value)
Definition: byteswap.h:108
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
#define USBBLK_COMMAND_SIGNATURE
Command block wrapper signature.
Definition: usbblk.h:50
struct usb_function * func
USB function.
Definition: usbblk.h:85
uint32_t tag
Tag.
Definition: usbblk.h:36
uint32_t len
Data transfer length.
Definition: usbblk.h:38
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:40
A persistent I/O buffer.
Definition: iobuf.h:38

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 202 of file usbblk.c.

202  {
203  struct usbblk_command *cmd = &usbblk->cmd;
204  struct io_buffer *iobuf;
205  size_t len;
206  int rc;
207 
208  /* Calculate length */
209  assert ( cmd->tag );
210  assert ( cmd->scsi.data_out != NULL );
211  assert ( cmd->offset < cmd->scsi.data_out_len );
212  len = ( cmd->scsi.data_out_len - cmd->offset );
213  if ( len > USBBLK_MAX_LEN )
215  assert ( ( len % usbblk->out.mtu ) == 0 );
216 
217  /* Allocate I/O buffer */
218  iobuf = alloc_iob ( len );
219  if ( ! iobuf ) {
220  rc = -ENOMEM;
221  goto err_alloc;
222  }
223 
224  /* Populate I/O buffer */
225  memcpy ( iob_put ( iobuf, len ),
226  ( cmd->scsi.data_out + cmd->offset ), len );
227 
228  /* Send data */
229  if ( ( rc = usb_stream ( &usbblk->out, iobuf, 0 ) ) != 0 ) {
230  DBGC ( usbblk, "USBBLK %s bulk OUT could not send data: %s\n",
231  usbblk->func->name, strerror ( rc ) );
232  goto err_stream;
233  }
234 
235  /* Consume data */
236  cmd->offset += len;
237 
238  return 0;
239 
240  err_stream:
241  free_iob ( iobuf );
242  err_alloc:
243  return rc;
244 }
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbblk.h:87
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:101
A USB mass storage command.
Definition: usbblk.h:68
#define iob_put(iobuf, len)
Definition: iobuf.h:125
const char * name
Name.
Definition: usb.h:676
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:153
#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:546
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:131
#define ENOMEM
Not enough space.
Definition: errno.h:535
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ring len
Length.
Definition: dwmac.h:231
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
#define USBBLK_MAX_LEN
Maximum length of USB data block.
Definition: usbblk.h:114
size_t mtu
Maximum transfer size.
Definition: usb.h:412
struct usb_function * func
USB function.
Definition: usbblk.h:85
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
A persistent I/O buffer.
Definition: iobuf.h:38

References alloc_iob(), assert(), cmd, usbblk_device::cmd, DBGC, ENOMEM, free_iob(), usbblk_device::func, iob_put, len, memcpy(), usb_endpoint::mtu, usb_function::name, NULL, usbblk_device::out, rc, strerror(), 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 252 of file usbblk.c.

252  {
253  struct usbblk_command *cmd = &usbblk->cmd;
254  int rc;
255 
256  /* Sanity checks */
257  assert ( cmd->tag );
258 
259  /* Refill endpoint */
260  while ( ( cmd->offset < cmd->scsi.data_out_len ) &&
261  ( usbblk->out.fill < USBBLK_MAX_FILL ) ) {
262  if ( ( rc = usbblk_out_data ( usbblk ) ) != 0 )
263  return rc;
264  }
265 
266  return 0;
267 }
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbblk.h:87
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:101
#define USBBLK_MAX_FILL
Maximum endpoint fill level.
Definition: usbblk.h:120
A USB mass storage command.
Definition: usbblk.h:68
static int usbblk_out_data(struct usbblk_device *usbblk)
Send bulk OUT data block.
Definition: usbblk.c:202
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
unsigned int fill
Buffer fill level.
Definition: usb.h:421
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 276 of file usbblk.c.

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

332  {
333  struct usbblk_command *cmd = &usbblk->cmd;
334 
335  /* Sanity checks */
336  assert ( cmd->tag );
337  assert ( cmd->scsi.data_in != NULL );
338  assert ( cmd->offset <= cmd->scsi.data_in_len );
339  assert ( len <= ( cmd->scsi.data_in_len - cmd->offset ) );
340 
341  /* Store data */
342  memcpy ( ( cmd->scsi.data_in + cmd->offset ), data, len );
343  cmd->offset += len;
344 
345  return 0;
346 }
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:101
A USB mass storage command.
Definition: usbblk.h:68
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ring len
Length.
Definition: dwmac.h:231
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29

References assert(), cmd, usbblk_device::cmd, data, len, memcpy(), and NULL.

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 356 of file usbblk.c.

357  {
358  struct usbblk_command *cmd = &usbblk->cmd;
359  const struct usbblk_status_wrapper *stat;
360 
361  /* Sanity checks */
362  assert ( cmd->tag );
363 
364  /* Validate length */
365  if ( len < sizeof ( *stat ) ) {
366  DBGC ( usbblk, "USBBLK %s bulk IN malformed status:\n",
367  usbblk->func->name );
368  DBGC_HDA ( usbblk, 0, data, len );
369  return -EIO;
370  }
371  stat = data;
372 
373  /* Validate signature */
374  if ( stat->signature != cpu_to_le32 ( USBBLK_STATUS_SIGNATURE ) ) {
375  DBGC ( usbblk, "USBBLK %s bulk IN invalid signature %08x:\n",
376  usbblk->func->name, le32_to_cpu ( stat->signature ) );
377  DBGC_HDA ( usbblk, 0, stat, sizeof ( *stat ) );
378  return -EIO;
379  }
380 
381  /* Validate tag */
382  if ( stat->tag != cmd->tag ) {
383  DBGC ( usbblk, "USBBLK %s bulk IN tag mismatch (got %08x, "
384  "expected %08x):\n",
385  usbblk->func->name, stat->tag, cmd->tag );
386  DBGC_HDA ( usbblk, 0, stat, sizeof ( *stat ) );
387  return -EIO;
388  }
389 
390  /* Check status */
391  if ( stat->status ) {
392  DBGC ( usbblk, "USBBLK %s bulk IN status %02x:\n",
393  usbblk->func->name, stat->status );
394  DBGC_HDA ( usbblk, 0, stat, sizeof ( *stat ) );
395  return -EIO;
396  }
397 
398  /* Check for residual data */
399  if ( stat->residue ) {
400  DBGC ( usbblk, "USBBLK %s bulk IN residue %#x:\n",
401  usbblk->func->name, le32_to_cpu ( stat->residue ) );
402  return -EIO;
403  }
404 
405  /* Mark command as complete */
406  usbblk_stop ( usbblk, 0 );
407 
408  return 0;
409 }
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:101
A USB mass storage command.
Definition: usbblk.h:68
const char * name
Name.
Definition: usb.h:676
uint32_t stat
Completion status.
Definition: dwmac.h:12
#define le32_to_cpu(value)
Definition: byteswap.h:114
#define DBGC(...)
Definition: compiler.h:505
static void usbblk_stop(struct usbblk_device *usbblk, int rc)
Stop SCSI command.
Definition: usbblk.c:554
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define DBGC_HDA(...)
Definition: compiler.h:506
ring len
Length.
Definition: dwmac.h:231
#define cpu_to_le32(value)
Definition: byteswap.h:108
#define USBBLK_STATUS_SIGNATURE
Command status wrapper signature.
Definition: usbblk.h:65
Command status wrapper.
Definition: usbblk.h:53
struct usb_function * func
USB function.
Definition: usbblk.h:85
#define EIO
Input/output error.
Definition: errno.h:434
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, cpu_to_le32, data, DBGC, DBGC_HDA, EIO, usbblk_device::func, le32_to_cpu, len, usb_function::name, stat, 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 417 of file usbblk.c.

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

References assert(), cmd, usbblk_device::cmd, usbblk_device::in, max, rc, stat, 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 449 of file usbblk.c.

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

525  {
526 
527  /* Refill bulk OUT endpoint */
528  usbblk_out_refill ( usbblk );
529 
530  /* Refill bulk IN endpoint */
531  usbblk_in_refill ( usbblk );
532 }
static int usbblk_in_refill(struct usbblk_device *usbblk)
Refill bulk IN endpoint.
Definition: usbblk.c:417
static int usbblk_out_refill(struct usbblk_device *usbblk)
Refill bulk OUT endpoint.
Definition: usbblk.c:252

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 580 of file usbblk.c.

581  {
582  struct usbblk_command *cmd = &usbblk->cmd;
583  int rc;
584 
585  /* Fail if command is in progress */
586  if ( cmd->tag ) {
587  rc = -EBUSY;
588  DBGC ( usbblk, "USBBLK %s cannot support multiple commands\n",
589  usbblk->func->name );
590  goto err_busy;
591  }
592 
593  /* Refuse bidirectional commands */
594  if ( scsicmd->data_in_len && scsicmd->data_out_len ) {
595  rc = -EOPNOTSUPP;
596  DBGC ( usbblk, "USBBLK %s cannot support bidirectional "
597  "commands\n", usbblk->func->name );
598  goto err_bidirectional;
599  }
600 
601  /* Sanity checks */
602  assert ( ! process_running ( &usbblk->process ) );
603  assert ( cmd->offset == 0 );
604 
605  /* Initialise command */
606  memcpy ( &cmd->scsi, scsicmd, sizeof ( cmd->scsi ) );
607  cmd->tag = ( USBBLK_TAG_MAGIC | ++usbblk_tag );
608 
609  /* Issue bulk OUT command */
610  if ( ( rc = usbblk_out_command ( usbblk ) ) != 0 )
611  goto err_command;
612 
613  /* Start refill process */
614  process_add ( &usbblk->process );
615 
616  return 0;
617 
618  err_command:
619  memset ( &usbblk->cmd, 0, sizeof ( usbblk->cmd ) );
620  err_bidirectional:
621  err_busy:
622  return rc;
623 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:101
struct process process
Command process.
Definition: usbblk.h:96
A USB mass storage command.
Definition: usbblk.h:68
size_t data_out_len
Data-out buffer length.
Definition: scsi.h:260
#define EBUSY
Device or resource busy.
Definition: errno.h:339
const char * name
Name.
Definition: usb.h:676
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:546
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:60
#define EOPNOTSUPP
Operation not supported on socket.
Definition: errno.h:605
static int process_running(struct process *process)
Check if process is running.
Definition: process.h:176
struct usb_function * func
USB function.
Definition: usbblk.h:85
#define USBBLK_TAG_MAGIC
Command tag magic.
Definition: usbblk.h:108
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:149
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 647 of file usbblk.c.

647  {
648  struct usbblk_command *cmd = &usbblk->cmd;
649 
650  /* Allow a single command if no command is currently in progress */
651  return ( cmd->tag ? 0 : 1 );
652 }
struct usbblk_command cmd
Current command (if any)
Definition: usbblk.h:101
A USB mass storage command.
Definition: usbblk.h:68
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 662 of file usbblk.c.

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

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 693 of file usbblk.c.

693  {
694 
695  /* Restart interfaces */
696  intfs_restart ( rc, &usbblk->scsi, &usbblk->data, NULL );
697 
698  /* Stop any in-progress command */
699  usbblk_stop ( usbblk, rc );
700 
701  /* Close endpoints */
702  usbblk_close ( usbblk );
703 
704  /* Flag as closed */
705  usbblk->opened = 0;
706 }
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:387
static void usbblk_stop(struct usbblk_device *usbblk, int rc)
Stop SCSI command.
Definition: usbblk.c:554
struct interface scsi
SCSI command-issuing interface.
Definition: usbblk.h:92
int opened
Device opened flag.
Definition: usbblk.h:98
static void usbblk_close(struct usbblk_device *usbblk)
Close endpoints.
Definition: usbblk.c:125
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
struct interface data
SCSI data interface.
Definition: usbblk.h:94

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 715 of file usbblk.c.

715  {
716 
717  return efi_usb_path ( usbblk->func );
718 }
EFI_DEVICE_PATH_PROTOCOL * efi_usb_path(struct usb_function *func)
Construct EFI device path for USB function.
Definition: efi_path.c:697
struct usb_function * func
USB function.
Definition: usbblk.h:85

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 746 of file usbblk.c.

746  {
747  struct usbblk_device *usbblk;
748 
749  /* Look for matching device */
750  list_for_each_entry ( usbblk, &usbblk_devices, list ) {
751  if ( strcmp ( usbblk->func->name, name ) == 0 )
752  return usbblk;
753  }
754 
755  return NULL;
756 }
const char * name
Definition: ath9k_hw.c:1986
const char * name
Name.
Definition: usb.h:676
A USB mass storage device.
Definition: usbblk.h:78
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:432
struct list_head list
List of devices.
Definition: usbblk.h:82
struct usb_function * func
USB function.
Definition: usbblk.h:85
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:174
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

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 765 of file usbblk.c.

765  {
766  static struct scsi_lun lun;
767  struct usbblk_device *usbblk;
768  int rc;
769 
770  /* Sanity check */
771  if ( ! uri->opaque )
772  return -EINVAL;
773 
774  /* Find matching device */
775  usbblk = usbblk_find ( uri->opaque );
776  if ( ! usbblk )
777  return -ENOENT;
778 
779  /* Fail if device is already open */
780  if ( usbblk->opened )
781  return -EBUSY;
782 
783  /* Open SCSI device */
784  if ( ( rc = scsi_open ( parent, &usbblk->scsi, &lun ) ) != 0 ) {
785  DBGC ( usbblk, "USBBLK %s could not open SCSI device: %s\n",
786  usbblk->func->name, strerror ( rc ) );
787  return rc;
788  }
789 
790  /* Mark as opened */
791  usbblk->opened = 1;
792 
793  return 0;
794 }
#define EINVAL
Invalid argument.
Definition: errno.h:429
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EBUSY
Device or resource busy.
Definition: errno.h:339
const char * name
Name.
Definition: usb.h:676
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:515
int scsi_open(struct interface *block, struct interface *scsi, struct scsi_lun *lun)
Open SCSI device.
Definition: scsi.c:985
struct interface scsi
SCSI command-issuing interface.
Definition: usbblk.h:92
A USB mass storage device.
Definition: usbblk.h:78
uint8_t lun
Logical Unit Number.
Definition: edd.h:33
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
struct usb_function * func
USB function.
Definition: usbblk.h:85
const char * opaque
Opaque part.
Definition: uri.h:71
static struct usbblk_device * usbblk_find(const char *name)
Find USB block device.
Definition: usbblk.c:746
int opened
Device opened flag.
Definition: usbblk.h:98
A Uniform Resource Identifier.
Definition: uri.h:65
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 816 of file usbblk.c.

817  {
818  struct usb_device *usb = func->usb;
819  struct usbblk_device *usbblk;
821  int rc;
822 
823  /* Allocate and initialise structure */
824  usbblk = zalloc ( sizeof ( *usbblk ) );
825  if ( ! usbblk ) {
826  rc = -ENOMEM;
827  goto err_alloc;
828  }
829  usbblk->func = func;
830  usb_endpoint_init ( &usbblk->out, usb, &usbblk_out_operations );
831  usb_endpoint_init ( &usbblk->in, usb, &usbblk_in_operations );
833  intf_init ( &usbblk->scsi, &usbblk_scsi_desc, &usbblk->refcnt );
834  intf_init ( &usbblk->data, &usbblk_data_desc, &usbblk->refcnt );
836  &usbblk->refcnt );
837 
838  /* Locate interface descriptor */
839  desc = usb_interface_descriptor ( config, func->interface[0], 0 );
840  if ( ! desc ) {
841  DBGC ( usbblk, "USBBLK %s missing interface descriptor\n",
842  usbblk->func->name );
843  rc = -ENOENT;
844  goto err_desc;
845  }
846 
847  /* Describe endpoints */
848  if ( ( rc = usb_endpoint_described ( &usbblk->out, config, desc,
849  USB_BULK_OUT, 0 ) ) != 0 ) {
850  DBGC ( usbblk, "USBBLK %s could not describe bulk OUT: %s\n",
851  usbblk->func->name, strerror ( rc ) );
852  goto err_out;
853  }
854  if ( ( rc = usb_endpoint_described ( &usbblk->in, config, desc,
855  USB_BULK_IN, 0 ) ) != 0 ) {
856  DBGC ( usbblk, "USBBLK %s could not describe bulk IN: %s\n",
857  usbblk->func->name, strerror ( rc ) );
858  goto err_in;
859  }
860 
861  /* Add to list of devices */
862  list_add_tail ( &usbblk->list, &usbblk_devices );
863 
864  usb_func_set_drvdata ( func, usbblk );
865  return 0;
866 
867  err_in:
868  err_out:
869  err_desc:
870  ref_put ( &usbblk->refcnt );
871  err_alloc:
872  return rc;
873 }
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbblk.h:87
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define USBBLK_MAX_FILL
Maximum endpoint fill level.
Definition: usbblk.h:120
struct process process
Command process.
Definition: usbblk.h:96
uint8_t interface[0]
List of interface numbers.
Definition: usb.h:697
const char * name
Name.
Definition: usb.h:676
#define DBGC(...)
Definition: compiler.h:505
static struct usb_endpoint_driver_operations usbblk_in_operations
Bulk IN endpoint operations.
Definition: usbblk.c:509
#define ENOENT
No such file or directory.
Definition: errno.h:515
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:242
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
#define ENOMEM
Not enough space.
Definition: errno.h:535
A USB interface descriptor.
Definition: usb.h:245
struct refcnt refcnt
Reference count.
Definition: usbblk.h:80
struct interface scsi
SCSI command-issuing interface.
Definition: usbblk.h:92
#define USB_BULK_OUT
Bulk OUT endpoint (internal) type.
Definition: usb.h:296
A USB mass storage device.
Definition: usbblk.h:78
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:94
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:617
static void usb_func_set_drvdata(struct usb_function *func, void *priv)
Set USB function driver private data.
Definition: usb.h:707
struct list_head list
List of devices.
Definition: usbblk.h:82
A USB device.
Definition: usb.h:723
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:144
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
#define USBBLK_MAX_LEN
Maximum length of USB data block.
Definition: usbblk.h:114
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:146
struct usb_device * usb
USB device.
Definition: usb.h:678
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:540
struct usb_endpoint in
Bulk IN endpoint.
Definition: usbblk.h:89
static struct interface_descriptor usbblk_data_desc
SCSI data interface descriptor.
Definition: usbblk.c:638
#define USB_BULK_IN
Bulk IN endpoint (internal) type.
Definition: usb.h:299
struct usb_function * func
USB function.
Definition: usbblk.h:85
static struct interface_descriptor usbblk_scsi_desc
SCSI command interface descriptor.
Definition: usbblk.c:730
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:204
static struct process_descriptor usbblk_process_desc
Refill process descriptor.
Definition: usbblk.c:535
static struct usb_endpoint_driver_operations usbblk_out_operations
Bulk OUT endpoint operations.
Definition: usbblk.c:312
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:107
struct interface data
SCSI data interface.
Definition: usbblk.h:94

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 880 of file usbblk.c.

880  {
881  struct usbblk_device *usbblk = usb_func_get_drvdata ( func );
882 
883  /* Remove from list of devices */
884  list_del ( &usbblk->list );
885 
886  /* Close all interfaces */
887  usbblk_scsi_close ( usbblk, -ENODEV );
888 
889  /* Shut down interfaces */
890  intfs_shutdown ( -ENODEV, &usbblk->scsi, &usbblk->data, NULL );
891 
892  /* Drop reference */
893  ref_put ( &usbblk->refcnt );
894 }
static void * usb_func_get_drvdata(struct usb_function *func)
Get USB function driver private data.
Definition: usb.h:718
void intfs_shutdown(int rc,...)
Shut down multiple object interfaces.
Definition: interface.c:327
static void usbblk_scsi_close(struct usbblk_device *usbblk, int rc)
Close SCSI interface.
Definition: usbblk.c:693
#define list_del(list)
Delete an entry from a list.
Definition: list.h:120
struct refcnt refcnt
Reference count.
Definition: usbblk.h:80
struct interface scsi
SCSI command-issuing interface.
Definition: usbblk.h:92
A USB mass storage device.
Definition: usbblk.h:78
struct list_head list
List of devices.
Definition: usbblk.h:82
#define ENODEV
No such device.
Definition: errno.h:510
struct usb_function * func
USB function.
Definition: usbblk.h:85
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:107
struct interface data
SCSI data interface.
Definition: usbblk.h:94

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:276

Bulk OUT endpoint operations.

Definition at line 312 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:449

Bulk IN endpoint operations.

Definition at line 509 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:18
A USB mass storage device.
Definition: usbblk.h:78
#define PROC_DESC(object_type, process, _step)
Define a process descriptor.
Definition: process.h:83
static void usbblk_step(struct usbblk_device *usbblk)
Refill endpoints.
Definition: usbblk.c:525

Refill process descriptor.

Definition at line 535 of file usbblk.c.

Referenced by usbblk_probe().

◆ usbblk_tag

uint16_t usbblk_tag
static

Next command tag.

Definition at line 546 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:250
static void usbblk_stop(struct usbblk_device *usbblk, int rc)
Stop SCSI command.
Definition: usbblk.c:554
A USB mass storage device.
Definition: usbblk.h:78
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:33

SCSI data interface operations.

Definition at line 633 of file usbblk.c.

◆ usbblk_data_desc

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

SCSI data interface descriptor.

Definition at line 638 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:250
size_t xfer_window(struct interface *intf)
Check flow control window.
Definition: xfer.c:117
static void usbblk_scsi_close(struct usbblk_device *usbblk, int rc)
Close SCSI interface.
Definition: usbblk.c:693
A SCSI command.
Definition: scsi.c:263
A USB mass storage device.
Definition: usbblk.h:78
static int usbblk_scsi_command(struct usbblk_device *usbblk, struct interface *data, struct scsi_cmd *scsicmd)
Issue SCSI command.
Definition: usbblk.c:662
#define EFI_INTF_OP
Definition: efi.h:374
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:33
static size_t usbblk_scsi_window(struct usbblk_device *usbblk)
Check SCSI command flow-control window.
Definition: usbblk.c:647
EFI_DEVICE_PATH_PROTOCOL * efi_describe(struct interface *intf)
Describe object as an EFI device path.
Definition: efi_path.c:920
static EFI_DEVICE_PATH_PROTOCOL * usbblk_efi_describe(struct usbblk_device *usbblk)
Describe as an EFI device path.
Definition: usbblk.c:715

SCSI command interface operations.

Definition at line 721 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:92
A USB mass storage device.
Definition: usbblk.h:78
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:81
static struct interface_operation usbblk_scsi_operations[]
SCSI command interface operations.
Definition: usbblk.c:721

SCSI command interface descriptor.

Definition at line 730 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:765

USB block device URI opener.

Definition at line 797 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:1373

Mass storage class device IDs.

Definition at line 897 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:25
#define USB_CLASS_ID(base, subclass, protocol)
Construct USB class ID.
Definition: usb.h:1389
Normal driver.
Definition: usb.h:1453
static void usbblk_remove(struct usb_function *func)
Remove device.
Definition: usbblk.c:880
#define USB_SUBCLASS_MSC_SCSI
SCSI command set subclass code.
Definition: usbblk.h:22
static int usbblk_probe(struct usb_function *func, struct usb_configuration_descriptor *config)
Probe device.
Definition: usbblk.c:816
#define USB_CLASS_MSC
Mass storage class code.
Definition: usbblk.h:19
static struct usb_device_id usbblk_ids[]
Mass storage class device IDs.
Definition: usbblk.c:897

Mass storage driver.

Definition at line 906 of file usbblk.c.