Ejemplo n.º 1
0
Archivo: device.go Proyecto: ebfe/gousb
// KernelDriverActive determines if a kernel driver is active on iface
func (d *Device) KernelDriverActive(iface uint8) (bool, error) {
	r := C.libusb_kernel_driver_active(d.handle, C.int(iface))
	if r < 0 {
		return false, usbError(r)
	}
	return r == 1, nil
}
Ejemplo n.º 2
0
// detachKernelDriver detaches any active kernel drivers, if supported by the platform.
// If there are any errors, like Context.ListDevices, only the final one will be returned.
func (d *Device) detachKernelDriver() (err error) {
	for _, cfg := range d.Configs {
		for _, iface := range cfg.Interfaces {
			switch activeErr := C.libusb_kernel_driver_active(d.handle, C.int(iface.Number)); activeErr {
			case C.LIBUSB_ERROR_NOT_SUPPORTED:
				// no need to do any futher checking, no platform support
				return
			case 0:
				continue
			case 1:
				switch detachErr := C.libusb_detach_kernel_driver(d.handle, C.int(iface.Number)); detachErr {
				case C.LIBUSB_ERROR_NOT_SUPPORTED:
					// shouldn't ever get here, should be caught by the outer switch
					return
				case 0:
					d.detached[iface.Number]++
				case C.LIBUSB_ERROR_NOT_FOUND:
					// this status is returned if libusb's driver is already attached to the device
					d.detached[iface.Number]++
				default:
					err = fmt.Errorf("usb: detach kernel driver: %s", usbError(detachErr))
				}
			default:
				err = fmt.Errorf("usb: active kernel driver check: %s", usbError(activeErr))
			}
		}
	}

	return
}
Ejemplo n.º 3
0
Archivo: usb.go Proyecto: thequux/gousb
func (i *Interface) IsKernelDriverActive() (bool, *UsbError) {
	v, err := decodeUsbError(C.libusb_kernel_driver_active(i.handle.handle, i.num))
	if err != nil {
		return false, err
	}
	return (v == 1), nil
}
Ejemplo n.º 4
0
Archivo: usb.go Proyecto: hanwen/usb
// Determine if a kernel driver is active on an interface.
func (h *DeviceHandle) KernelDriverActive(ifaceNum byte) (bool, error) {
	ret := C.libusb_kernel_driver_active(h.me(), C.int(ifaceNum))
	if ret == 0 {
		return false, nil
	}
	if ret == 1 {
		return true, nil
	}
	return false, toErr(ret)
}
Ejemplo n.º 5
0
// Detach from kernel if kernel attached a driver to the device.
// If it's attached, C.libusb_kernel_driver_active returns 1.
func (d *Device) DetachKernelDriver(iface uint8) error {
	if C.libusb_kernel_driver_active(d.handle, C.int(iface)) != 1 {
		return nil
	}
	errno := C.libusb_detach_kernel_driver(d.handle, C.int(iface))
	if errno != 0 {
		return usbError(errno)
	}
	return nil
}
Ejemplo n.º 6
0
func Start(color chan byte, output chan guinput.KeyEvent) *C.struct_libusb_device_handle {

	C.libusb_init(nil)
	fmt.Println(KEYMAP[0x21])

	key_device_handle := C.libusb_open_device_with_vid_pid(nil, 0x046d, 0xc22d)

	//detach any necessary kernel drivers from MY keyboard
	if C.libusb_kernel_driver_active(key_device_handle, 0) == 1 {
		log.Print("kernel driver active on main interface")
		e := C.libusb_detach_kernel_driver(key_device_handle, 0)
		if e != 0 {
			log.Fatal("Can't detach kernel driver")
		}
	}
	if C.libusb_kernel_driver_active(key_device_handle, 1) == 1 {
		fmt.Println("kernel driver active")
		e := C.libusb_detach_kernel_driver(key_device_handle, 1)
		if e != 0 {
			log.Fatal("Can't detach kernel driver")
		}
	}

	//Claim the interfaces we'll be listening on
	r := C.libusb_claim_interface(key_device_handle, 0)
	if r != 0 {
		log.Fatal("Can't claim main interface")
	}
	r = C.libusb_claim_interface(key_device_handle, 1)
	if r != 0 {
		log.Fatal("Can't claim special interface")
	}

	log.Print("Starting libusb goroutines\n")

	go ColorChange(key_device_handle, color)
	//go LCDChange(key_device_handle, lcd)
	go NormalKeyMonitor(key_device_handle, output)
	go SpecialKeyMonitor(key_device_handle, output)

	log.Print("Libusb Goroutines started\n")
	return key_device_handle
}
Ejemplo n.º 7
0
func StartLibUsb() error {
	Connected = true

	key_device_handle := C.libusb_open_device_with_vid_pid(nil, 0x046d, 0xc22d)
	if key_device_handle != nil {

		if C.libusb_kernel_driver_active(key_device_handle, 1) == 1 {
			e := C.libusb_detach_kernel_driver(key_device_handle, 1)
			if e != 0 {
				log.Fatal("Can't detach kernel driver")
			}
		}

		r := C.libusb_claim_interface(key_device_handle, 1)
		if r != 0 {
			log.Fatal("Can't claim special interface")
		}

		keyboardHandle = key_device_handle

		return nil
	}
	return errors.New("could not open driver")
}
Ejemplo n.º 8
0
func (d *Device) OpenEndpoint(conf, iface, setup, epoint uint8) (Endpoint, error) {
	end := &endpoint{
		Device: d,
	}

	var setAlternate bool
	for _, c := range d.Configs {
		if c.Config != conf {
			continue
		}
		debug.Printf("found conf: %#v\n", c)
		for _, i := range c.Interfaces {
			if i.Number != iface {
				continue
			}
			debug.Printf("found iface: %#v\n", i)
			for i, s := range i.Setups {
				if s.Alternate != setup {
					continue
				}
				setAlternate = i != 0

				debug.Printf("found setup: %#v [default: %v]\n", s, !setAlternate)
				for _, e := range s.Endpoints {
					debug.Printf("ep %02x search: %#v\n", epoint, s)
					if e.Address != epoint {
						continue
					}
					end.InterfaceSetup = s
					end.EndpointInfo = e
					switch tt := TransferType(e.Attributes) & TRANSFER_TYPE_MASK; tt {
					case TRANSFER_TYPE_BULK:
						end.xfer = bulk_xfer
					case TRANSFER_TYPE_INTERRUPT:
						end.xfer = interrupt_xfer
					case TRANSFER_TYPE_ISOCHRONOUS:
						end.xfer = isochronous_xfer
					default:
						return nil, fmt.Errorf("usb: %s transfer is unsupported", tt)
					}
					goto found
				}
				return nil, fmt.Errorf("usb: unknown endpoint %02x", epoint)
			}
			return nil, fmt.Errorf("usb: unknown setup %02x", setup)
		}
		return nil, fmt.Errorf("usb: unknown interface %02x", iface)
	}
	return nil, fmt.Errorf("usb: unknown configuration %02x", conf)

found:

	// Set the configuration
	var activeConf C.int
	if errno := C.libusb_get_configuration(d.handle, &activeConf); errno < 0 {
		return nil, fmt.Errorf("usb: getcfg: %s", usbError(errno))
	}
	if int(activeConf) != int(conf) {
		if errno := C.libusb_set_configuration(d.handle, C.int(conf)); errno < 0 {
			return nil, fmt.Errorf("usb: setcfg: %s", usbError(errno))
		}
	}

	// Enable detachment of kernel driver if platform supported
	if errno := C.libusb_kernel_driver_active(d.handle, C.int(iface)); errno == 1 {
		if errno := C.libusb_detach_kernel_driver(d.handle, C.int(iface)); errno != 0 {
			return nil, fmt.Errorf("usb: detach: %s", usbError(errno))
		}
	}

	// Claim the interface
	if errno := C.libusb_claim_interface(d.handle, C.int(iface)); errno < 0 {
		return nil, fmt.Errorf("usb: claim: %s", usbError(errno))
	}

	// Increment the claim count
	d.lock.Lock()
	d.claimed[iface]++
	d.lock.Unlock() // unlock immediately because the next calls may block

	// Choose the alternate
	if setAlternate {
		if errno := C.libusb_set_interface_alt_setting(d.handle, C.int(iface), C.int(setup)); errno < 0 {
			debug.Printf("altsetting error: %s", usbError(errno))
			return nil, fmt.Errorf("usb: setalt: %s", usbError(errno))
		}
	}

	return end, nil
}