Disable USB Mouse/Joystick From Keyboard/Mouse/Joystick Trio on Teensy?

Status
Not open for further replies.

brane

New member
Hi!

Is there a way to "block" or "disable" the USB mouse/joystick aspects of the Teensy program so that it will look like a keyboard only and not a keyboard/mouse/joystick ?

In the beginning
I had an Arduino programmed to work as a USB keyboard when plugged into my Raspberry Pi. At the time, there was also a USB mouse plugged into the Raspberry Pi. Both were detected in a program that I use. That is, when running the application, both the USB keyboard and USB mouse were recognized.

The Move to Teensy
I've replaced my Arduino with a Teensy 3.1. I made a few code changes (TeensyDuino) for the native USB keyboard support, and the USB keyboard events still work great. However, when I start MAME, it no longer detects my USB mouse.

A Clue?
I suspect this is because the Teensy is recognized not as just a USB keyboard, but as a keyboard, and a mouse, and a joystick. So, I think MAME detects the Teensy mouse and Teensy joystick, and decides not to bother looking for additional USB mice. Sure enough, when I look at everything under /dev with the Teensy connected via USB, I see /dev/input/by-id/usb-Teensyduino_* links with names ending in *joystick and *mouse as well as *kbd . (long names shortened for readability)

Please advise on how to disable joystick/mouse Teensy USB types when compiling as a USB keyboard.

Thanks!
 
Is there a way to "block" or "disable" the USB mouse/joystick aspects of the Teensy program so that it will look like a keyboard only and not a keyboard/mouse/joystick ?

Yes, it's possible, but you'll need to edit the USB code. This can't be done from within Arduino.

The file you want is in hardware/teensy/cores/teensy3/usb_desc.h

Comment out the stuff you don't need. Don't forget to update the numbers in the "OFFSET" lines.

Automatic upload will probably break, so be prepared to have to push the button on Teensy after you've uploaded USB code that's different than what the tools expect.

You're using Linux, which is good. Linux has a pretty solid USB stack. If you or anyone reading this tries the same on Windows, always save your work first! Also, Windows tends to cache previous detection of your USB device in the Windows Registry, which is quite silly since it has to read all the info every time you reconnect. On Windows, it's often necessary to increment bcdDevice in usb_desc.c to get Windows to update the registry with your new device info. Linux is so much better.
 
Yes, it's possible, but you'll need to edit the USB code. This can't be done from within Arduino.

The file you want is in hardware/teensy/cores/teensy3/usb_desc.h

Comment out the stuff you don't need. Don't forget to update the numbers in the "OFFSET" lines.

Automatic upload will probably break, so be prepared to have to push the button on Teensy after you've uploaded USB code that's different than what the tools expect.

You're using Linux, which is good. Linux has a pretty solid USB stack. If you or anyone reading this tries the same on Windows, always save your work first! Also, Windows tends to cache previous detection of your USB device in the Windows Registry, which is quite silly since it has to read all the info every time you reconnect. On Windows, it's often necessary to increment bcdDevice in usb_desc.c to get Windows to update the registry with your new device info. Linux is so much better.

Thanks! I'll try it. Meanwhile, I've found a temporary solution that works for the Raspberry Pi: I just deleted the stuff under the /dev/inputs directories mentioned in the original post (both the links and what they pointed to) that was related to Teensy mouse/joystick, and everything worked as I hoped. But the usb_desc.h fix will be more elegant, since I am wanting my Teensy project to work on any computer that can use a USB keyboard. So, I'll try it soon.
 
Thanks again, Paul! I got it to work. I also had a lot of help from this page: http://forum.pjrc.com/archive/index.php/t-26024.html

I went a little beyond the basics and actually added a "Serial + Keyboard" option to the Arduino Tools > USB Type selection.

In case anyone else is trying to do this, here's exactly what I changed/added in 5 files:

Code:
diff ./hardware/teensy/boards.txt.org ./[B]hardware/teensy/boards.txt[/B]
45a46,47
> teensy31.menu.usb.serialkeyboard.name=Serial + Keyboard
> teensy31.menu.usb.serialkeyboard.build.define0=-DUSB_SERIAL_KEYBOARD

Code:
diff ./hardware/teensy/cores/teensy3/usb_inst.cpp.org ./[B]hardware/teensy/cores/teensy3/usb_inst.cpp[/B]
46a47,51
> #ifdef USB_SERIAL_KEYBOARD
> usb_serial_class Serial;
> usb_keyboard_class Keyboard;
> #endif
> 
73c78
< #if defined(USB_SERIAL) || defined(USB_SERIAL_HID)
---
> #if defined(USB_SERIAL) || defined(USB_SERIAL_HID) || defined(USB_SERIAL_KEYBOARD)

Code:
diff ./hardware/teensy/cores/teensy3/usb_keyboard.h.org ./[B]hardware/teensy/cores/teensy3/usb_keyboard.h[/B]
36c36
< #if defined(USB_HID) || defined(USB_SERIAL_HID)
---
> #if defined(USB_HID) || defined(USB_SERIAL_HID) || defined(USB_SERIAL_KEYBOARD)

Code:
diff ./hardware/teensy/cores/teensy3/usb_serial.h.org ./[B]hardware/teensy/cores/teensy3/usb_serial.h[/B]
34c34
< #if defined(USB_SERIAL) || defined(USB_SERIAL_HID)
---
> #if defined(USB_SERIAL) || defined(USB_SERIAL_HID) || defined(USB_SERIAL_KEYBOARD)

Added to hardware/teensy/cores/teensy3/usb_desc.h:
Code:
#elif defined(USB_SERIAL_KEYBOARD)
  #define VENDOR_ID             0x16C0
  #define PRODUCT_ID            0x0489
  #define DEVICE_CLASS          0xEF
  #define DEVICE_SUBCLASS       0x02
  #define DEVICE_PROTOCOL       0x01
  #define MANUFACTURER_NAME     {'T','e','e','n','s','y','d','u','i','n','o'}
  #define MANUFACTURER_NAME_LEN 11
  #define PRODUCT_NAME          {'S','e','r','i','a','l','/','K','e','y','b','o','a','r','d'}
  #define PRODUCT_NAME_LEN      15
  #define EP0_SIZE              64
  #define NUM_ENDPOINTS         4
  #define NUM_USB_BUFFERS       30
  #define NUM_INTERFACE         3
  #define CDC_IAD_DESCRIPTOR    1
  #define CDC_STATUS_INTERFACE  0
  #define CDC_DATA_INTERFACE    1       // Serial
  #define CDC_ACM_ENDPOINT      2
  #define CDC_RX_ENDPOINT       3
  #define CDC_TX_ENDPOINT       4
  #define CDC_ACM_SIZE          16
  #define CDC_RX_SIZE           64
  #define CDC_TX_SIZE           64
  #define KEYBOARD_INTERFACE    2       // Keyboard
  #define KEYBOARD_ENDPOINT     1
  #define KEYBOARD_SIZE         8
  #define KEYBOARD_INTERVAL     1
  #define KEYBOARD_DESC_OFFSET  (9+8 + 9+5+5+4+5+7+9+7+7 + 9)
  #define CONFIG_DESC_SIZE          (9+8 + 9+5+5+4+5+7+9+7+7 + 9+9+7)
  #define ENDPOINT1_CONFIG      ENDPOINT_TRANSIMIT_ONLY
  #define ENDPOINT2_CONFIG      ENDPOINT_TRANSIMIT_ONLY
  #define ENDPOINT3_CONFIG      ENDPOINT_RECEIVE_ONLY
  #define ENDPOINT4_CONFIG      ENDPOINT_TRANSIMIT_ONLY
 
How would I proceed to do the same in Teensy 2.0?I assume I should go to \hardware\teensy\avr\cores\teensy folder and not to \hardware\teensy\avr\cores\teensy3, but I can't find the usb_desc.h, usb_inst.cpp, usb_keyboard.h files.
I want to have a usb type that is only USB Keyboard.Any advices?
 
How would I proceed to do the same in Teensy 2.0?

It's not nearly as easy as with Teensy 3.x.

You'll need to edit the code in the usb_hid folder. The USB config descriptor is how your PC learns the USB device. Delete the interfaces you don't want. Update the number of interfaces. You'll probably also have to adjust the numbers for the length of the array.

You can probably leave a lot of the unused code. As long as your PC understands the config descriptor, it will only ask for things that descriptor says exist.

If you get stuck, perhaps this very old code can help. It's a much earlier version of the code which eventually became Teensyduino.

https://www.pjrc.com/teensy/usb_keyboard.html

With enough fiddling, perhaps you can get this to work. But it's not easy like the newer code. If you get stuck, maybe just buying a Teensy LC might be the most effective way to get this done?
 
I'll try editing the usb config descriptor

You'll probably also have to adjust the numbers for the length of the array.
Can you give me more details about that?I'm not experienced in editing such kind of files.

Also I've already downloaded the usb keyboard code you suggested but I havent been able to compile anything with it.I'll take a closer look at the code and hope that I could figure something out.
 
Fiddling with the low-level USB code really isn't a beginner level project. That's why so much work went into making usb_desc.h able to reconfigure most things in the newer code base.
 
I'll probably buy a teensy LC.What folder is for teensy LC so I can edit the correct usb_desc.h?teensy3?
Yes, it uses the teensy3 stuff. The OFFSET stuff described above is gone from the defines, you don't need to fiddle with that anymore. Changing the descriptors is pleasantly simple now.
 
Edit hardware/teensy/avr/cores/teensy3/usb_desc.h for Teensy LC or 3.x. The file has instructions in the comments. It's much easier. You do still have to do some simple things like get the number of interfaces define to match how many interfaces you enable, but it's all fairly simple. The rest of the code adapts automatically do changes in that 1 file.
 
Edit hardware/teensy/avr/cores/teensy3/usb_desc.h for Teensy LC or 3.x. The file has instructions in the comments. It's much easier. You do still have to do some simple things like get the number of interfaces define to match how many interfaces you enable, but it's all fairly simple. The rest of the code adapts automatically do changes in that 1 file.

im trying to accomplish the same thing. i just want to disable mouse but still use keyboard and joystick. i have followed your instructions but it doesn't seem to disable the mouse. could you check if i did it right. all i have done is comment out the mouse section and lower the number of interfaces by 1.

Code:
#elif defined(USB_HID)
  #define VENDOR_ID		0x16C0
  #define PRODUCT_ID		0x0482
  #define MANUFACTURER_NAME	{'T','e','e','n','s','y','d','u','i','n','o'}
  #define MANUFACTURER_NAME_LEN	11
  #define PRODUCT_NAME		{'K','e','y','b','o','a','r','d','/','M','o','u','s','e','/','J','o','y','s','t','i','c','k'}
  #define PRODUCT_NAME_LEN	23
  #define EP0_SIZE		64
  #define NUM_ENDPOINTS         6
  #define NUM_USB_BUFFERS	24
  #define NUM_INTERFACE		4
  #define SEREMU_INTERFACE      2	// Serial emulation
  #define SEREMU_TX_ENDPOINT    1
  #define SEREMU_TX_SIZE        64
  #define SEREMU_TX_INTERVAL    1
  #define SEREMU_RX_ENDPOINT    2
  #define SEREMU_RX_SIZE        32
  #define SEREMU_RX_INTERVAL    2
  #define KEYBOARD_INTERFACE    0	// Keyboard
  #define KEYBOARD_ENDPOINT     3
  #define KEYBOARD_SIZE         8
  #define KEYBOARD_INTERVAL     1
  #define KEYMEDIA_INTERFACE    4	// Keyboard Media Keys
  #define KEYMEDIA_ENDPOINT     6
  #define KEYMEDIA_SIZE         8
  #define KEYMEDIA_INTERVAL     4
  //#define MOUSE_INTERFACE       1	// Mouse
  //#define MOUSE_ENDPOINT        5
  //#define MOUSE_SIZE            8
  //#define MOUSE_INTERVAL        1
  #define JOYSTICK_INTERFACE    3	// Joystick
  #define JOYSTICK_ENDPOINT     4
  #define JOYSTICK_SIZE         12	//  12 = normal, 64 = extreme joystick
  #define JOYSTICK_INTERVAL     2
  #define ENDPOINT1_CONFIG	ENDPOINT_TRANSIMIT_ONLY
  #define ENDPOINT2_CONFIG	ENDPOINT_RECEIVE_ONLY
  #define ENDPOINT3_CONFIG	ENDPOINT_TRANSIMIT_ONLY
  #define ENDPOINT4_CONFIG	ENDPOINT_TRANSIMIT_ONLY
  #define ENDPOINT5_CONFIG	ENDPOINT_TRANSIMIT_ONLY
  #define ENDPOINT6_CONFIG	ENDPOINT_TRANSIMIT_ONLY
 
so i have created a custom joystick for a game called star citizen but in the menu the mouse seems to go crazy, it flickers, its hard to explain. im hoping disabling the mouse totally will solve the problem.
This seems simple so i may have done it right and the problem is something else. The game is a alpha game.
 
Last edited:
Sorry to revive an old thread but the relevant info is all here.

Paul, you mentioned that modifying usb_desc.h will break automatic upload, which I have indeed observed while implementing changes similar to what Brane did above (I did Serial + Joystick). How difficult would it be to get automatic upload working? Would it require recompiling Teensyduino?
 
How difficult would it be to get automatic upload working?

Depends on how you edit usb_desc.h, which things you remove or change.

If you delete the CDC serial or HID seremu interface that receives the auto-reboot request, then hopefully it should be obvious that auto-reboot can never work.
 
Thanks, you're right: automatic upload only wasn't working for a previous iteration where I accidentally had one of the serial interfaces undefined. With the correct Serial + Joystick definitions it still uploads fine.

Edit: In case anyone wants to do Serial + Joystick, here is what I added to usb_desc.h:

Code:
#elif defined(USB_SERIAL_JOYSTICK)
  #define VENDOR_ID		0x16C0
  #define PRODUCT_ID		0x0491
  #define DEVICE_CLASS		0xEF
  #define DEVICE_SUBCLASS	0x02
  #define DEVICE_PROTOCOL	0x01
  #define MANUFACTURER_NAME	{'T','e','e','n','s','y','d','u','i','n','o'}
  #define MANUFACTURER_NAME_LEN	11
  #define PRODUCT_NAME		{'S','e','r','i','a','l','/','J','o','y','s','t','i','c','k'}
  #define PRODUCT_NAME_LEN	15
  #define EP0_SIZE		64
  #define NUM_ENDPOINTS		7
  #define NUM_USB_BUFFERS	30
  #define NUM_INTERFACE		3
  #define CDC_IAD_DESCRIPTOR	1
  #define CDC_STATUS_INTERFACE	0
  #define CDC_DATA_INTERFACE	1	// Serial
  #define CDC_ACM_ENDPOINT	1
  #define CDC_RX_ENDPOINT       2
  #define CDC_TX_ENDPOINT       3
  #define CDC_ACM_SIZE          16
  #define CDC_RX_SIZE           64
  #define CDC_TX_SIZE           64
  #define JOYSTICK_INTERFACE    2	// Joystick
  #define JOYSTICK_ENDPOINT     4
  #define JOYSTICK_SIZE         12	//  12 = normal, 64 = extreme joystick
  #define JOYSTICK_INTERVAL     1
  #define ENDPOINT1_CONFIG	ENDPOINT_TRANSMIT_ONLY
  #define ENDPOINT2_CONFIG	ENDPOINT_RECEIVE_ONLY
  #define ENDPOINT3_CONFIG	ENDPOINT_TRANSMIT_ONLY
  #define ENDPOINT4_CONFIG	ENDPOINT_TRANSMIT_ONLY

Additionally, the syntax for boards.txt has changed slightly since the example above. My addition looked like this:

Code:
teensy31.menu.usb.serialjoystick=Serial + Joystick
teensy31.menu.usb.serialjoystick.build.usbtype=USB_SERIAL_JOYSTICK
 
Status
Not open for further replies.
Back
Top