libusb-glue.c

来自「Media transfer protocol implementation o」· C语言 代码 · 共 864 行 · 第 1/2 页

C
864
字号
/* *  libusb-glue.c * *  Created by Richard Low on 24/12/2005. (as mtp-utils.c) *  Modified by Linus Walleij 2006-03-06 *  (Notice that Anglo-Saxons use little-endian dates and Swedes use big-endian dates.) * * This file adds some utils (many copied from ptpcam.c from libptp2) to * use MTP devices. Include mtp-utils.h to use any of the ptp/mtp functions. * */#include "libmtp.h"#include "libusb-glue.h"#include "util.h"#include "ptp.h"#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <usb.h>/* To enable debug prints, switch on this *///#define ENABLE_USB_BULK_DEBUG/* OUR APPLICATION USB URB (2MB) ;) */#define PTPCAM_USB_URB		2097152/* this must not be too short - the original 4000 was not long   enough for big file transfers. I imagine the player spends a    bit of time gearing up to receiving lots of data. This also makes   connecting/disconnecting more reliable */#define USB_TIMEOUT		10000#define USB_CAPTURE_TIMEOUT	20000/* USB control message data phase direction */#ifndef USB_DP_HTD#define USB_DP_HTD		(0x00 << 7)	/* host to device */#endif#ifndef USB_DP_DTH#define USB_DP_DTH		(0x01 << 7)	/* device to host */#endif/* USB Feature selector HALT */#ifndef USB_FEATURE_HALT#define USB_FEATURE_HALT	0x00#endif/* * MTP device list, trying real bad to get all devices into * this list by stealing from everyone I know. */static const LIBMTP_device_entry_t mtp_device_table[] = {    /*   * Creative Technology   * Initially the Creative devices was all we supported so these are   * the most thoroughly tested devices.   */  { "Creative Zen Vision", 0x041e, 0x411f, DEVICE_FLAG_NONE },  { "Creative Portable Media Center", 0x041e, 0x4123, DEVICE_FLAG_NONE },  { "Creative Zen Xtra (MTP mode)", 0x041e, 0x4128, DEVICE_FLAG_NONE },  { "Second generation Dell DJ", 0x041e, 0x412f, DEVICE_FLAG_NONE },  { "Creative Zen Micro (MTP mode)", 0x041e, 0x4130, DEVICE_FLAG_NONE },  { "Creative Zen Touch (MTP mode)", 0x041e, 0x4131, DEVICE_FLAG_NONE },  { "Dell Pocket DJ (MTP mode)", 0x041e, 0x4132, DEVICE_FLAG_NONE },  { "Creative Zen Sleek (MTP mode)", 0x041e, 0x4137, DEVICE_FLAG_NONE },  { "Creative Zen MicroPhoto", 0x041e, 0x413c, DEVICE_FLAG_NONE },  { "Creative Zen Sleek Photo", 0x041e, 0x413d, DEVICE_FLAG_NONE },  { "Creative Zen Vision:M", 0x041e, 0x413e, DEVICE_FLAG_NONE },  // Reported by marazm@o2.pl  { "Creative Zen V", 0x041e, 0x4150, DEVICE_FLAG_NONE },  // Reported by danielw@iinet.net.au  { "Creative Zen Vision:M (DVP-HD0004)", 0x041e, 0x4151, DEVICE_FLAG_NONE },  // Reported by Darel on the XNJB forums  { "Creative Zen V Plus", 0x041e, 0x4152, DEVICE_FLAG_NONE },  { "Creative Zen Vision W", 0x041e, 0x4153, DEVICE_FLAG_NONE },  /*   * Samsung   * We suspect that more of these are dual mode.   */  // From libgphoto2  { "Samsung YH-820", 0x04e8, 0x502e, DEVICE_FLAG_NONE },  // Contributed by polux2001@users.sourceforge.net  { "Samsung YH-925", 0x04e8, 0x502f, DEVICE_FLAG_NONE },  // Contributed by anonymous person on SourceForge  { "Samsung YP-T7J", 0x04e8, 0x5047, DEVICE_FLAG_NONE },  // Reported by cstrickler@gmail.com  { "Samsung YP-U2J (YP-U2JXB/XAA)", 0x04e8, 0x5054, DEVICE_FLAG_NONE },  // Reported by Andrew Benson  { "Samsung YP-F2J", 0x04e8, 0x5057, DEVICE_FLAG_DUALMODE },  // Reported by Patrick <skibler@gmail.com>  { "Samsung YP-K5", 0x04e8, 0x505a, DEVICE_FLAG_NONE },  // Reported by Matthew Wilcox <matthew@wil.cx>  { "Samsung Yepp T9", 0x04e8, 0x507f, DEVICE_FLAG_NONE },  // From a rouge .INF file  { "Samsung YH-999 Portable Media Center", 0x04e8, 0x5a0f, DEVICE_FLAG_NONE },  /*   * Intel   */  { "Intel Bandon Portable Media Center", 0x045e, 0x00c9, DEVICE_FLAG_NONE },  /*   * JVC   */  // From Mark Veinot  { "JVC Alneo XA-HD500", 0x04f1, 0x6105, DEVICE_FLAG_NONE },  /*   * Philips   */  // From libgphoto2 source  { "Philips HDD6320", 0x0471, 0x01eb, DEVICE_FLAG_NONE },  { "Philips HDD6320/00", 0x0471, 0x014b, DEVICE_FLAG_NONE },  // Anonymous SourceForge user  { "Philips HDD1630/17", 0x0471, 0x014c, DEVICE_FLAG_NONE },  // From Gerhard Mekenkamp  { "Philips GoGear Audio", 0x0471, 0x0165, DEVICE_FLAG_NONE },  /*   * SanDisk   */  // Reported by Brian Robison  { "SanDisk Sansa m240", 0x0781, 0x7400, DEVICE_FLAG_NONE },  // Reported by tangent_@users.sourceforge.net  { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_NONE },  // From libgphoto2 source  { "SanDisk Sansa e200", 0x0781, 0x7420, DEVICE_FLAG_NONE },  // Reported by gonkflea@users.sourceforge.net  { "SanDisk Sansa e260", 0x0781, 0x7420, DEVICE_FLAG_NONE },  /*   * iRiver   * we assume that PTP_OC_MTP_GetObjPropList is essentially   * broken on all iRiver devices. Please test on your device   * if you believe it isn't!   */  { "iRiver Portable Media Center", 0x1006, 0x4002, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },  { "iRiver Portable Media Center", 0x1006, 0x4003, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },  // From libgphoto2 source  { "iRiver T10", 0x4102, 0x1113, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },  { "iRiver T20 FM", 0x4102, 0x1114, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },  { "iRiver U10", 0x4102, 0x1116, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },  { "iRiver T10", 0x4102, 0x1117, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },  { "iRiver T20", 0x4102, 0x1118, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },  { "iRiver T30", 0x4102, 0x1119, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },  // Reported by David Wolpoff  { "iRiver T10 2GB", 0x4102, 0x1120, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },  // Reported by Adam Torgerson  { "iRiver Clix", 0x4102, 0x112a, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },  // Reported by Scott Call  { "iRiver H10 20GB", 0x4102, 0x2101, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },  { "iRiver H10", 0x4102, 0x2102, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },  /*   * Dell   */  { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE },    /*   * Toshiba   */  { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE },  { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE },  // From libgphoto2  { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE },  // Reported by Rob Brown  { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE },  /*   * Archos   */  // Reported by gudul1@users.sourceforge.net  { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE },  /*   * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad   * This unit is falsely detected as USB mass storage in Linux   * prior to kernel 2.6.19 (fixed by patch from Alan Stern)   * so on older kernels special care is needed to remove the   * USB mass storage driver that erroneously binds to the device   * interface.   */  { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER},    /*   * Microsoft   */  // Reported by Farooq Zaman  { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE },     /*   * Sirius   */  { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE }  };static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);int ptpcam_usb_timeout = USB_TIMEOUT;// Local functionsstatic struct usb_bus* init_usb();static struct usb_device *probe_usb_bus_for_mtp_devices(void);static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber);static struct usb_device* find_device (int busn, int devicen, short force);static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep);static void clear_stall(PTP_USB* ptp_usb);static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);static short ptp_write_func (unsigned char *bytes, unsigned int size, void *data);static short ptp_read_func (unsigned char *bytes, unsigned int size, void *data, unsigned int *readbytes);static short ptp_check_int (unsigned char *bytes, unsigned int size, void *data, unsigned int *rlen);static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);int get_device_list(LIBMTP_device_entry_t ** const devices, int * const numdevs){  *devices = (LIBMTP_device_entry_t *) &mtp_device_table;  *numdevs = mtp_device_table_size;  return 0;}static struct usb_bus* init_usb(){  usb_init();  usb_find_busses();  usb_find_devices();  return (usb_get_busses());}/** * Check for the Microsoft OS device descriptor and returns device struct * if the device is MTP-compliant. The function will only recognize * a single device connected to the USB bus. * * @return an MTP-compliant USB device if one was found, else NULL. */static struct usb_device *probe_usb_bus_for_mtp_devices(void){  struct usb_bus *bus;    bus = init_usb();  for (; bus; bus = bus->next) {    struct usb_device *dev;        for (dev = bus->devices; dev; dev = dev->next) {      usb_dev_handle *devh;      unsigned char buf[1024], cmd;      int ret;        devh = usb_open(dev);      if (devh == NULL) {	continue;      }      // Read the special descripor, if possible...      ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));            if (ret < 10) {	// printf("Device: VID %04x/PID %04x: no extended device property...\n",	//       dev->descriptor.idVendor,	//       dev->descriptor.idProduct);	usb_close(devh);	continue;      }            // It is atleast 10 bytes...      if (!((buf[2] == 'M') && (buf[4]=='S') && (buf[6]=='F') && (buf[8]=='T'))) {	printf("This is not a Microsoft MTP descriptor...\n");	printf("Device response to read device property 0xee:\n");	data_dump_ascii (stdout, buf, ret, 0);	usb_close(devh);	continue;      }            cmd = buf[16];      ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, 			     cmd, 0, 4, (char *) buf, sizeof(buf), 1000);      if (ret == -1) {	//printf("Decice could not respond to control message 1.\n");	usb_close(devh);	continue;      }            if (ret > 0x15) {	if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {	  printf("The device has a Microsoft device descriptor, but it's not MTP.\n");	  printf("This is not an MTP device. Presumable it is USB mass storage\n");	  printf("with some additional Janus (DRM) support.\n");	  printf("Device response to control message 1:\n");	  data_dump_ascii (stdout, buf, ret, 0);	  continue;	}      } else {	// Not MTP or broken	continue;      }      ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, 			     cmd, 0, 5, (char *) buf, sizeof(buf), 1000);      if (ret == -1) {	//printf("Device could not respond to control message 2.\n");	usb_close(devh);	// Return the device anyway, it said previously it was MTP, right?	return dev;      }            if (ret > 0x15) {	if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {	  printf("This device does not respond with MTP characteristics on\n");	  printf("the second device property read (0x05), but will be regarded\n");	  printf("as MTP anyway.\n");	  printf("Device response to control message 2:\n");	  data_dump_ascii (stdout, buf, ret, 0);	}      }            usb_close(devh);      // We can return the device here, it will be the first.       // If it was not MTP, the loop continues before it reaches this point.      return dev;    }  }  // If nothing was found we end up here.  return NULL;}/** * Detect the MTP device descriptor and return the VID and PID * of the first device found. This is a very low-level function * which is intended for use with <b>udev</b> or other hotplug * mechanisms. The idea is that a script may want to know if the * just plugged-in device was an MTP device or not. * @param vid the Vendor ID (VID) of the first device found. * @param pid the Product ID (PID) of the first device found. * @return the number of detected devices or -1 if the call *         was unsuccessful. */int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid){  struct usb_device *dev = probe_usb_bus_for_mtp_devices();  if (dev == NULL) {    return 0;  }  *vid = dev->descriptor.idVendor;  *pid = dev->descriptor.idProduct;  return 1;}/** * This routine just dumps out low-level * USB information about the current device. * @param ptp_usb the USB device to get information from. */void dump_usbinfo(PTP_USB *ptp_usb){  int res;  struct usb_device *dev;#ifdef LIBUSB_HAS_GET_DRIVER_NP  char devname[0x10];    devname[0] = '\0';  res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname));  if (devname[0] != '\0') {    printf("   Using kernel interface \"%s\"\n", devname);  }#endif  dev = usb_device(ptp_usb->handle);  printf("   bcdUSB: %d\n", dev->descriptor.bcdUSB);  printf("   bDeviceClass: %d\n", dev->descriptor.bDeviceClass);  printf("   bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);  printf("   bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);  printf("   idVendor: %04x\n", dev->descriptor.idVendor);  printf("   idProduct: %04x\n", dev->descriptor.idProduct);  printf("   IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);  printf("   OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);  printf("   Device flags: 0x%08x\n", ptp_usb->device_flags);  // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...}// Based on same function on library.c in libgphoto2#define CONTEXT_BLOCK_SIZE	0x100000static shortptp_read_func (unsigned char *bytes, unsigned int size, void *data, unsigned int *readbytes){  PTP_USB *ptp_usb = (PTP_USB *)data;  int toread = 0;  int result = 0;  int curread = 0;  /* Split into small blocks. Too large blocks (>1x MB) would   * timeout.   */  while (curread < size) {    toread = size - curread;    if (toread > CONTEXT_BLOCK_SIZE)      toread = CONTEXT_BLOCK_SIZE;        result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep,(char *)(bytes+curread), toread, ptpcam_usb_timeout);    if (result == 0) {      result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep,(char *)(bytes+curread), toread, ptpcam_usb_timeout);    }    if (result < 0)      return PTP_ERROR_IO;#ifdef ENABLE_USB_BULK_DEBUG    printf("<==USB IN\n");    data_dump_ascii (stdout,(bytes+curread),result,16);#endif    curread += result;    if (result < toread) /* short reads are common */      break;  }  // Increase counters, call callback  if (ptp_usb->callback_active) {    ptp_usb->current_transfer_complete += curread;    if (ptp_usb->current_transfer_complete > ptp_usb->current_transfer_total) {      // Fishy... but some commands have unpredictable lengths.      // send last update and disable callback.      ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;      ptp_usb->callback_active = 0;    }    if (ptp_usb->current_transfer_callback != NULL) {      (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,						ptp_usb->current_transfer_total,						ptp_usb->current_transfer_callback_data);    }  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?