📄 st5481_usb.c
字号:
/* * Driver for ST5481 USB ISDN modem * * Author Frode Isaksen * Copyright 2001 by Frode Isaksen <fisaksen@bewan.com> * 2001 by Kai Germaschewski <kai.germaschewski@gmx.de> * * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * */#include <linux/init.h>#include <linux/usb.h>#include <linux/slab.h>#include "st5481.h"static int st5481_isoc_flatten(struct urb *urb);/* ====================================================================== * control pipe *//* * Send the next endpoint 0 request stored in the FIFO. * Called either by the completion or by usb_ctrl_msg. */static void usb_next_ctrl_msg(struct urb *urb, struct st5481_adapter *adapter){ struct st5481_ctrl *ctrl = &adapter->ctrl; int r_index; if (test_and_set_bit(0, &ctrl->busy)) { return; } if ((r_index = fifo_remove(&ctrl->msg_fifo.f)) < 0) { test_and_clear_bit(0,&ctrl->busy); return; } urb->setup_packet = (unsigned char *)&ctrl->msg_fifo.data[r_index]; DBG(1,"request=0x%02x,value=0x%04x,index=%x", ((struct ctrl_msg *)urb->setup_packet)->dr.bRequest, ((struct ctrl_msg *)urb->setup_packet)->dr.wValue, ((struct ctrl_msg *)urb->setup_packet)->dr.wIndex); // Prepare the URB urb->dev = adapter->usb_dev; SUBMIT_URB(urb, GFP_ATOMIC);}/* * Asynchronous endpoint 0 request (async version of usb_control_msg). * The request will be queued up in a FIFO if the endpoint is busy. */static void usb_ctrl_msg(struct st5481_adapter *adapter, u8 request, u8 requesttype, u16 value, u16 index, ctrl_complete_t complete, void *context){ struct st5481_ctrl *ctrl = &adapter->ctrl; int w_index; struct ctrl_msg *ctrl_msg; if ((w_index = fifo_add(&ctrl->msg_fifo.f)) < 0) { WARN("control msg FIFO full"); return; } ctrl_msg = &ctrl->msg_fifo.data[w_index]; ctrl_msg->dr.bRequestType = requesttype; ctrl_msg->dr.bRequest = request; ctrl_msg->dr.wValue = cpu_to_le16p(&value); ctrl_msg->dr.wIndex = cpu_to_le16p(&index); ctrl_msg->dr.wLength = 0; ctrl_msg->complete = complete; ctrl_msg->context = context; usb_next_ctrl_msg(ctrl->urb, adapter);}/* * Asynchronous endpoint 0 device request. */void st5481_usb_device_ctrl_msg(struct st5481_adapter *adapter, u8 request, u16 value, ctrl_complete_t complete, void *context){ usb_ctrl_msg(adapter, request, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, value, 0, complete, context);}/* * Asynchronous pipe reset (async version of usb_clear_halt). */void st5481_usb_pipe_reset(struct st5481_adapter *adapter, u_char pipe, ctrl_complete_t complete, void *context){ DBG(1,"pipe=%02x",pipe); usb_ctrl_msg(adapter, USB_REQ_CLEAR_FEATURE, USB_DIR_OUT | USB_RECIP_ENDPOINT, 0, pipe, complete, context);}/* Physical level functions*/void st5481_ph_command(struct st5481_adapter *adapter, unsigned int command){ DBG(8,"command=%s", ST5481_CMD_string(command)); st5481_usb_device_ctrl_msg(adapter, TXCI, command, NULL, NULL);}/* * The request on endpoint 0 has completed. * Call the user provided completion routine and try * to send the next request. */static void usb_ctrl_complete(struct urb *urb, struct pt_regs *regs){ struct st5481_adapter *adapter = urb->context; struct st5481_ctrl *ctrl = &adapter->ctrl; struct ctrl_msg *ctrl_msg; if (unlikely(urb->status < 0)) { switch (urb->status) { case -ENOENT: case -ESHUTDOWN: case -ECONNRESET: DBG(1,"urb killed status %d", urb->status); return; // Give up default: WARN("urb status %d",urb->status); break; } } ctrl_msg = (struct ctrl_msg *)urb->setup_packet; if (ctrl_msg->dr.bRequest == USB_REQ_CLEAR_FEATURE) { /* Special case handling for pipe reset */ le16_to_cpus(&ctrl_msg->dr.wIndex); /* toggle is reset on clear */ usb_settoggle(adapter->usb_dev, ctrl_msg->dr.wIndex & ~USB_DIR_IN, (ctrl_msg->dr.wIndex & USB_DIR_IN) == 0, 0); } if (ctrl_msg->complete) ctrl_msg->complete(ctrl_msg->context); clear_bit(0, &ctrl->busy); // Try to send next control message usb_next_ctrl_msg(urb, adapter); return;}/* ====================================================================== * interrupt pipe *//* * The interrupt endpoint will be called when any * of the 6 registers changes state (depending on masks). * Decode the register values and schedule a private event. * Called at interrupt. */static void usb_int_complete(struct urb *urb, struct pt_regs *regs){ u8 *data = urb->transfer_buffer; u8 irqbyte; struct st5481_adapter *adapter = urb->context; int j; int status; switch (urb->status) { case 0: /* success */ break; case -ECONNRESET: case -ENOENT: case -ESHUTDOWN: /* this urb is terminated, clean up */ DBG(2, "urb shutting down with status: %d", urb->status); return; default: WARN("nonzero urb status received: %d", urb->status); goto exit; } DBG_PACKET(2, data, INT_PKT_SIZE); if (urb->actual_length == 0) { goto exit; } irqbyte = data[MPINT]; if (irqbyte & DEN_INT) FsmEvent(&adapter->d_out.fsm, EV_DOUT_DEN, NULL); if (irqbyte & DCOLL_INT) FsmEvent(&adapter->d_out.fsm, EV_DOUT_COLL, NULL); irqbyte = data[FFINT_D]; if (irqbyte & OUT_UNDERRUN) FsmEvent(&adapter->d_out.fsm, EV_DOUT_UNDERRUN, NULL); if (irqbyte & OUT_DOWN);// printk("OUT_DOWN\n"); irqbyte = data[MPINT]; if (irqbyte & RXCI_INT) FsmEvent(&adapter->l1m, data[CCIST] & 0x0f, NULL); for (j = 0; j < 2; j++) adapter->bcs[j].b_out.flow_event |= data[FFINT_B1 + j]; urb->actual_length = 0;exit: status = usb_submit_urb (urb, GFP_ATOMIC); if (status) WARN("usb_submit_urb failed with result %d", status);}/* ====================================================================== * initialization */int st5481_setup_usb(struct st5481_adapter *adapter){ struct usb_device *dev = adapter->usb_dev; struct st5481_ctrl *ctrl = &adapter->ctrl; struct st5481_intr *intr = &adapter->intr; struct usb_interface *intf; struct usb_host_interface *altsetting = NULL; struct usb_host_endpoint *endpoint; int status; struct urb *urb; u8 *buf; DBG(2,""); if ((status = usb_reset_configuration (dev)) < 0) { WARN("reset_configuration failed,status=%d",status); return status; } intf = usb_ifnum_to_if(dev, 0); if (intf) altsetting = usb_altnum_to_altsetting(intf, 3); if (!altsetting) return -ENXIO; // Check if the config is sane if ( altsetting->desc.bNumEndpoints != 7 ) { WARN("expecting 7 got %d endpoints!", altsetting->desc.bNumEndpoints); return -EINVAL; } // The descriptor is wrong for some early samples of the ST5481 chip altsetting->endpoint[3].desc.wMaxPacketSize = __constant_cpu_to_le16(32); altsetting->endpoint[4].desc.wMaxPacketSize = __constant_cpu_to_le16(32); // Use alternative setting 3 on interface 0 to have 2B+D if ((status = usb_set_interface (dev, 0, 3)) < 0) { WARN("usb_set_interface failed,status=%d",status); return status; } // Allocate URB for control endpoint urb = usb_alloc_urb(0, GFP_KERNEL); if (!urb) { return -ENOMEM; } ctrl->urb = urb; // Fill the control URB usb_fill_control_urb (urb, dev, usb_sndctrlpipe(dev, 0), NULL, NULL, 0, usb_ctrl_complete, adapter); fifo_init(&ctrl->msg_fifo.f, ARRAY_SIZE(ctrl->msg_fifo.data)); // Allocate URBs and buffers for interrupt endpoint urb = usb_alloc_urb(0, GFP_KERNEL); if (!urb) { return -ENOMEM; } intr->urb = urb; buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL); if (!buf) { return -ENOMEM; } endpoint = &altsetting->endpoint[EP_INT-1]; // Fill the interrupt URB usb_fill_int_urb(urb, dev, usb_rcvintpipe(dev, endpoint->desc.bEndpointAddress), buf, INT_PKT_SIZE, usb_int_complete, adapter, endpoint->desc.bInterval); return 0;}/* * Release buffers and URBs for the interrupt and control * endpoint. */void st5481_release_usb(struct st5481_adapter *adapter){ struct st5481_intr *intr = &adapter->intr; struct st5481_ctrl *ctrl = &adapter->ctrl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -