in meta-facebook/meta-fby3/recipes-kernel/exar1420/files/xr_usb_serial_common.c [1295:1796]
static int xr_usb_serial_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_cdc_union_desc *union_header = NULL;
struct usb_cdc_country_functional_desc *cfd = NULL;
unsigned char *buffer = intf->altsetting->extra;
int buflen = intf->altsetting->extralen;
struct usb_interface *control_interface;
struct usb_interface *data_interface;
struct usb_endpoint_descriptor *epctrl = NULL;
struct usb_endpoint_descriptor *epread = NULL;
struct usb_endpoint_descriptor *epwrite = NULL;
struct usb_device *usb_dev = interface_to_usbdev(intf);
struct xr_usb_serial *xr_usb_serial;
int minor;
int ctrlsize, readsize;
u8 *buf;
u8 ac_management_function = 0;
u8 call_management_function = 0;
int call_interface_num = -1;
int data_interface_num = -1;
unsigned long quirks;
int num_rx_buf;
int i;
int combined_interfaces = 0;
struct device *tty_dev;
int rv = -ENOMEM;
#ifdef CONFIG_GPIOLIB
int gpiochip_base;
#endif
/* normal quirks */
quirks = (unsigned long)id->driver_info;
if (quirks == IGNORE_DEVICE)
return -ENODEV;
num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : XR_USB_SERIAL_NR;
dev_dbg(&intf->dev, "USB_device_id idVendor:%04x, idProduct %04x\n",id->idVendor,id->idProduct);
/* handle quirks deadly to normal probing*/
if (quirks == NO_UNION_NORMAL) {
data_interface = usb_ifnum_to_if(usb_dev, 1);
control_interface = usb_ifnum_to_if(usb_dev, 0);
goto skip_normal_probe;
}
/* normal probing*/
if (!buffer) {
dev_err(&intf->dev, "Weird descriptor references\n");
return -EINVAL;
}
if (!buflen) {
if (intf->cur_altsetting->endpoint &&
intf->cur_altsetting->endpoint->extralen &&
intf->cur_altsetting->endpoint->extra) {
dev_dbg(&intf->dev,
"Seeking extra descriptors on endpoint\n");
buflen = intf->cur_altsetting->endpoint->extralen;
buffer = intf->cur_altsetting->endpoint->extra;
} else {
dev_err(&intf->dev,
"Zero length descriptor references\n");
return -EINVAL;
}
}
while (buflen > 0) {
if (buffer[1] != USB_DT_CS_INTERFACE) {
dev_err(&intf->dev, "skipping garbage\n");
goto next_desc;
}
switch (buffer[2]) {
case USB_CDC_UNION_TYPE: /* we've found it */
if (union_header) {
dev_err(&intf->dev, "More than one "
"union descriptor, skipping ...\n");
goto next_desc;
}
union_header = (struct usb_cdc_union_desc *)buffer;
break;
case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
cfd = (struct usb_cdc_country_functional_desc *)buffer;
break;
case USB_CDC_HEADER_TYPE: /* maybe check version */
break; /* for now we ignore it */
case USB_CDC_ACM_TYPE:
ac_management_function = buffer[3];
break;
case USB_CDC_CALL_MANAGEMENT_TYPE:
call_management_function = buffer[3];
call_interface_num = buffer[4];
if ((quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3)
dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
break;
default:
/* there are LOTS more CDC descriptors that
* could legitimately be found here.
*/
dev_dbg(&intf->dev, "Ignoring descriptor: "
"type %02x, length %d\n",
buffer[2], buffer[0]);
break;
}
next_desc:
buflen -= buffer[0];
buffer += buffer[0];
}
if (!union_header) {
if (call_interface_num > 0) {
dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
/* quirks for Droids MuIn LCD */
if (quirks & NO_DATA_INTERFACE)
data_interface = usb_ifnum_to_if(usb_dev, 0);
else
data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
control_interface = intf;
} else {
if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
dev_dbg(&intf->dev,"No union descriptor, giving up\n");
return -ENODEV;
} else {
dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
combined_interfaces = 1;
control_interface = data_interface = intf;
goto look_for_collapsed_interface;
}
}
} else {
control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
if (!control_interface || !data_interface) {
dev_dbg(&intf->dev, "no interfaces\n");
return -ENODEV;
}
}
if (data_interface_num != call_interface_num)
dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
if (control_interface == data_interface) {
/* some broken devices designed for windows work this way */
dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
combined_interfaces = 1;
/* a popular other OS doesn't use it */
quirks |= NO_CAP_LINE;
if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
return -EINVAL;
}
look_for_collapsed_interface:
for (i = 0; i < 3; i++) {
struct usb_endpoint_descriptor *ep;
ep = &data_interface->cur_altsetting->endpoint[i].desc;
if (usb_endpoint_is_int_in(ep))
epctrl = ep;
else if (usb_endpoint_is_bulk_out(ep))
epwrite = ep;
else if (usb_endpoint_is_bulk_in(ep))
epread = ep;
else
return -EINVAL;
}
if (!epctrl || !epread || !epwrite)
return -ENODEV;
else
goto made_compressed_probe;
}
skip_normal_probe:
/*workaround for switched interfaces */
if (data_interface->cur_altsetting->desc.bInterfaceClass
!= CDC_DATA_INTERFACE_TYPE) {
if (control_interface->cur_altsetting->desc.bInterfaceClass
== CDC_DATA_INTERFACE_TYPE) {
struct usb_interface *t;
dev_dbg(&intf->dev,
"Your device has switched interfaces.\n");
t = control_interface;
control_interface = data_interface;
data_interface = t;
} else {
return -EINVAL;
}
}
/* Accept probe requests only for the control interface */
if (!combined_interfaces && intf != control_interface)
return -ENODEV;
if (!combined_interfaces && usb_interface_claimed(data_interface)) {
/* valid in this context */
dev_dbg(&intf->dev, "The data interface isn't available\n");
return -EBUSY;
}
if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
control_interface->cur_altsetting->desc.bNumEndpoints == 0)
return -EINVAL;
epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
epread = &data_interface->cur_altsetting->endpoint[0].desc;
epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
/* workaround for switched endpoints */
if (!usb_endpoint_dir_in(epread)) {
/* descriptors are swapped */
struct usb_endpoint_descriptor *t;
dev_dbg(&intf->dev,
"The data interface has switched endpoints\n");
t = epread;
epread = epwrite;
epwrite = t;
}
made_compressed_probe:
dev_dbg(&intf->dev, "interfaces are valid\n");
xr_usb_serial = kzalloc(sizeof(struct xr_usb_serial), GFP_KERNEL);
if (xr_usb_serial == NULL) {
dev_err(&intf->dev, "out of memory (xr_usb_serial kzalloc)\n");
goto alloc_fail;
}
minor = xr_usb_serial_alloc_minor(xr_usb_serial);
if (minor == XR_USB_SERIAL_TTY_MINORS) {
dev_err(&intf->dev, "no more free xr_usb_serial devices\n");
kfree(xr_usb_serial);
return -ENODEV;
}
ctrlsize = usb_endpoint_maxp(epctrl);
readsize = usb_endpoint_maxp(epread) *
(quirks == SINGLE_RX_URB ? 1 : 2);
xr_usb_serial->combined_interfaces = combined_interfaces;
xr_usb_serial->writesize = usb_endpoint_maxp(epwrite) * 20;
xr_usb_serial->control = control_interface;
xr_usb_serial->data = data_interface;
xr_usb_serial->minor = minor;
xr_usb_serial->dev = usb_dev;
xr_usb_serial->ctrl_caps = ac_management_function;
if (quirks & NO_CAP_LINE)
xr_usb_serial->ctrl_caps &= ~USB_CDC_CAP_LINE;
xr_usb_serial->ctrlsize = ctrlsize;
xr_usb_serial->readsize = readsize;
xr_usb_serial->rx_buflimit = num_rx_buf;
INIT_WORK(&xr_usb_serial->work, xr_usb_serial_softint);
spin_lock_init(&xr_usb_serial->write_lock);
spin_lock_init(&xr_usb_serial->read_lock);
mutex_init(&xr_usb_serial->mutex);
xr_usb_serial->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
xr_usb_serial->is_int_ep = usb_endpoint_xfer_int(epread);
if (xr_usb_serial->is_int_ep)
xr_usb_serial->bInterval = epread->bInterval;
tty_port_init(&xr_usb_serial->port);
xr_usb_serial->port.ops = &xr_usb_serial_port_ops;
xr_usb_serial->DeviceVendor = id->idVendor;
xr_usb_serial->DeviceProduct = id->idProduct;
#if 0
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1410)
{//map the serial port A B C D to blocknum 0 1 2 3 for the xr21v141x device
xr_usb_serial->channel = epwrite->bEndpointAddress - 1;
}
else if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1420)
{//map the serial port A B C D to blocknum 0 2 4 6 for the xr21B142x device
xr_usb_serial->channel = (epwrite->bEndpointAddress - 4)*2;
}
else
{
xr_usb_serial->channel = epwrite->bEndpointAddress;
}
#else
xr_usb_serial->channel = epwrite->bEndpointAddress;
dev_dbg(&intf->dev, "epwrite->bEndpointAddress =%d\n",epwrite->bEndpointAddress);
#endif
buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &xr_usb_serial->ctrl_dma);
if (!buf) {
dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n");
goto alloc_fail2;
}
xr_usb_serial->ctrl_buffer = buf;
if (xr_usb_serial_write_buffers_alloc(xr_usb_serial) < 0) {
dev_err(&intf->dev, "out of memory (write buffer alloc)\n");
goto alloc_fail4;
}
xr_usb_serial->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
if (!xr_usb_serial->ctrlurb) {
dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
goto alloc_fail5;
}
for (i = 0; i < num_rx_buf; i++) {
struct xr_usb_serial_rb *rb = &(xr_usb_serial->read_buffers[i]);
struct urb *urb;
rb->base = usb_alloc_coherent(xr_usb_serial->dev, readsize, GFP_KERNEL,
&rb->dma);
if (!rb->base) {
dev_err(&intf->dev, "out of memory "
"(read bufs usb_alloc_coherent)\n");
goto alloc_fail6;
}
rb->index = i;
rb->instance = xr_usb_serial;
urb = usb_alloc_urb(0, GFP_KERNEL);
if (!urb) {
dev_err(&intf->dev,
"out of memory (read urbs usb_alloc_urb)\n");
goto alloc_fail6;
}
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
urb->transfer_dma = rb->dma;
if (xr_usb_serial->is_int_ep) {
usb_fill_int_urb(urb, xr_usb_serial->dev,
xr_usb_serial->rx_endpoint,
rb->base,
xr_usb_serial->readsize,
xr_usb_serial_read_bulk_callback, rb,
xr_usb_serial->bInterval);
} else {
usb_fill_bulk_urb(urb, xr_usb_serial->dev,
xr_usb_serial->rx_endpoint,
rb->base,
xr_usb_serial->readsize,
xr_usb_serial_read_bulk_callback, rb);
}
xr_usb_serial->read_urbs[i] = urb;
__set_bit(i, &xr_usb_serial->read_urbs_free);
}
for (i = 0; i < XR_USB_SERIAL_NW; i++) {
struct xr_usb_serial_wb *snd = &(xr_usb_serial->wb[i]);
snd->urb = usb_alloc_urb(0, GFP_KERNEL);
if (snd->urb == NULL) {
dev_err(&intf->dev,
"out of memory (write urbs usb_alloc_urb)\n");
goto alloc_fail7;
}
if (usb_endpoint_xfer_int(epwrite))
usb_fill_int_urb(snd->urb, usb_dev,
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
#else
usb_sndintpipe(usb_dev, epwrite->bEndpointAddress),
#endif
NULL, xr_usb_serial->writesize, xr_usb_serial_write_bulk, snd, epwrite->bInterval);
else
usb_fill_bulk_urb(snd->urb, usb_dev,
usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
NULL, xr_usb_serial->writesize, xr_usb_serial_write_bulk, snd);
snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
snd->instance = xr_usb_serial;
}
usb_set_intfdata(intf, xr_usb_serial);
i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
if (i < 0)
goto alloc_fail7;
if (cfd) { /* export the country data */
xr_usb_serial->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
if (!xr_usb_serial->country_codes)
goto skip_countries;
xr_usb_serial->country_code_size = cfd->bLength - 4;
memcpy(xr_usb_serial->country_codes, (u8 *)&cfd->wCountyCode0,
cfd->bLength - 4);
xr_usb_serial->country_rel_date = cfd->iCountryCodeRelDate;
i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
if (i < 0) {
kfree(xr_usb_serial->country_codes);
xr_usb_serial->country_codes = NULL;
xr_usb_serial->country_code_size = 0;
goto skip_countries;
}
i = device_create_file(&intf->dev,
&dev_attr_iCountryCodeRelDate);
if (i < 0) {
device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
kfree(xr_usb_serial->country_codes);
xr_usb_serial->country_codes = NULL;
xr_usb_serial->country_code_size = 0;
goto skip_countries;
}
}
skip_countries:
usb_fill_int_urb(xr_usb_serial->ctrlurb, usb_dev,
usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
xr_usb_serial->ctrl_buffer, ctrlsize, xr_usb_serial_ctrl_irq, xr_usb_serial,
/* works around buggy devices */
epctrl->bInterval ? epctrl->bInterval : 0xff);
xr_usb_serial->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
xr_usb_serial->ctrlurb->transfer_dma = xr_usb_serial->ctrl_dma;
dev_info(&intf->dev, "ttyXR_USB_SERIAL%d: USB XR_USB_SERIAL device\n", minor);
xr_usb_serial_pre_setup(xr_usb_serial);
xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout);
xr_usb_serial->line.dwDTERate = cpu_to_le32(9600);
xr_usb_serial->line.bDataBits = 8;
xr_usb_serial_set_line(xr_usb_serial, &xr_usb_serial->line);
usb_driver_claim_interface(&xr_usb_serial_driver, data_interface, xr_usb_serial);
usb_set_intfdata(data_interface, xr_usb_serial);
usb_get_intf(control_interface);
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
tty_register_device(xr_usb_serial_tty_driver, minor, &control_interface->dev);
#else
tty_dev = tty_port_register_device(&xr_usb_serial->port, xr_usb_serial_tty_driver, minor,
&control_interface->dev);
if (IS_ERR(tty_dev)) {
rv = PTR_ERR(tty_dev);
goto alloc_fail8;
}
#endif
#ifdef CONFIG_GPIOLIB
/* Setup GPIO cotroller */
gpiochip_base = 0;
xr_usb_serial->xr_gpio.owner = THIS_MODULE;
xr_usb_serial->xr_gpio.label = dev_name(&control_interface->dev);
xr_usb_serial->xr_gpio.direction_input = xr_usb_gpio_dir_input;
xr_usb_serial->xr_gpio.get = xr_usb_gpio_get;
xr_usb_serial->xr_gpio.direction_output = xr_usb_gpio_dir_output;
xr_usb_serial->xr_gpio.set = xr_usb_gpio_set;
xr_usb_serial->xr_gpio.base = gpiochip_base;
xr_usb_serial->xr_gpio.ngpio = 10;
xr_usb_serial->xr_gpio.can_sleep = 1;
rv = gpiochip_add(&xr_usb_serial->xr_gpio);
if (rv != 0) {
// gpiochip numbers not available, start from 0
xr_usb_serial->xr_gpio.base = 0;
}
while (rv != 0) {
xr_usb_serial->xr_gpio.base += 10;
if (xr_usb_serial->xr_gpio.base > 502) {
// max gpio number = 512
// we ran out of gpios??
break;
}
rv = gpiochip_add(&xr_usb_serial->xr_gpio);
}
xr_usb_serial->rv_gpio_created = rv;
if (rv == 0) {
dev_dbg(&xr_usb_serial->control->dev, "gpiochip%d added",
xr_usb_serial->xr_gpio.base);
} else {
dev_dbg(&xr_usb_serial->control->dev, "failed to add gpiochip\n");
}
#endif
return 0;
alloc_fail8:
if (xr_usb_serial->country_codes) {
device_remove_file(&xr_usb_serial->control->dev,
&dev_attr_wCountryCodes);
device_remove_file(&xr_usb_serial->control->dev,
&dev_attr_iCountryCodeRelDate);
}
device_remove_file(&xr_usb_serial->control->dev, &dev_attr_bmCapabilities);
alloc_fail7:
usb_set_intfdata(intf, NULL);
for (i = 0; i < XR_USB_SERIAL_NW; i++)
usb_free_urb(xr_usb_serial->wb[i].urb);
alloc_fail6:
for (i = 0; i < num_rx_buf; i++)
usb_free_urb(xr_usb_serial->read_urbs[i]);
xr_usb_serial_read_buffers_free(xr_usb_serial);
usb_free_urb(xr_usb_serial->ctrlurb);
alloc_fail5:
xr_usb_serial_write_buffers_free(xr_usb_serial);
alloc_fail4:
usb_free_coherent(usb_dev, ctrlsize, xr_usb_serial->ctrl_buffer, xr_usb_serial->ctrl_dma);
alloc_fail2:
xr_usb_serial_release_minor(xr_usb_serial);
kfree(xr_usb_serial);
alloc_fail:
return rv;
}