static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)
{
struct usb_skel *dev;
int retval = 0;
struct urb *urb = NULL;
char *buf = NULL;
size_t writesize = min(count, (size_t)MAX_TRANSFER);
dev = (struct usb_skel *)file->private_data;
/* verify that we actually have some data to write */
if (count == 0) **实际上 我们 有数据需要 写入
goto exit;
/* limit the number of URBs in flight to stop a user from using up all RAM */
**限制urb的数量,防止 一个用户用完所有的 RAM
if (down_interruptible(&dev->limit_sem)) {
retval = -ERESTARTSYS;
goto exit;
}
spin_lock_irq(&dev->err_lock);
if ((retval = dev->errors) < 0) { ** errors dev_skel定义 最后请求库 ??
/* any error is reported once */ 任何错误 被报告一次
dev->errors = 0;
/* to preserve notifications about reset */ **保留没有 重启 注意项、通知
retval = (retval == -EPIPE) ? retval : -EIO;
}
spin_unlock_irq(&dev->err_lock);
if (retval < 0)
goto error;
** 创建 urb、urb的缓冲区、将数据复制给urb
/* create a urb, and a buffer for it, and copy the data to the urb */
urb = usb_alloc_urb(0, GFP_KERNEL);
if (!urb) {
retval = -ENOMEM;
goto error;
}
buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL, &urb->transfer_dma);
if (!buf) {
retval = -ENOMEM;
goto error;
}
if (copy_from_user(buf, user_buffer, writesize)) {
retval = -EFAULT;
goto error;
}
/* this lock makes sure we don't submit URBs to gone devices */
***这个锁 使能确定 我们 不能 提交到已经运行的设备
mutex_lock(&dev->io_mutex);
if (!dev->interface) { /* disconnect() was called */
mutex_unlock(&dev->io_mutex);
retval = -ENODEV;
goto error;
}
/* initialize the urb properly */ **适当的 urb 初始化
usb_fill_bulk_urb(urb, dev->udev,
usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
buf, writesize, skel_write_bulk_callback, dev);
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; **状态
usb_anchor_urb(urb, &dev->submitted); **不知道 什么作用 ???
/* send the data out the bulk port */ **发送数据到批量端口
retval = usb_submit_urb(urb, GFP_KERNEL); **urb 提交按给 usb core
mutex_unlock(&dev->io_mutex);
if (retval) {
err("%s - failed submitting write urb, error %d", __func__, retval);
goto error_unanchor;
}
/* release our reference to this urb, the USB core will eventually free it entirely */
usb_free_urb(urb); ** 释放我们对这个urb 的作用 usb core将最终完全的 释放他
return writesize;
error_unanchor:
usb_unanchor_urb(urb);
error:
if (urb) {
usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma);
usb_free_urb(urb);
}
up(&dev->limit_sem);
exit:
return retval;
}