[usb] Make iothread termination reliable
[usb transport] Establish SIGUSR1 handler even when running as a plugin

The I/O thread management relies on sending SIGUSR1 to wake up
threads that are blocked on I/O. This only works if a handler has
been established in the receiving thread, otherwise it'll just
kill the process.
[usb transport] Fix potential segfault in input buffer handling

The fix is not ideal but Works For Now.
[usb transport] Make sure InterruptWriterThread actually exits

If the InterruptWriterThread was in the wrong part of its loop
then it might not have noticed the main thread calling exitThread.
[usb transport] code cleanup: tidy ControlReaderThread loop

The ControlReaderThread had an unnecessary nested loop. Flattening it
to one loop makes its error handler more similar to that of the other
threads and thus easier to understand.
[usb transport] Fix deactivate()

MTP deactivation wasn't properly shutting down the control thread,
which led to the main thread hanging in wait().
[usb transport] Respond to BIND and UNBIND events

Use BIND to open the /dev/mtp/ep* devices and start the threads.
Use UNBIND to stop the threads, close the devices and reset the thread states.

This results in a cleaner environment for the next connection.
[usb transport] Handle ESHUTDOWN during write by dropping the packet

The bulkwriter thread is only active while it's sending data, which
means it's sending a response to something. A shutdown in the middle
of that will mean that the host won't expect the response anymore,
so drop the data and return a failure code.
[mtp] Abort segmented getObject response if session was closed

If a session was closed (for example by unplugging the USB) while
sendObjectSegmented() was active, it would keep trying to send more
segments every time the connection was re-established. This interfered
with attempts to open a new session.

Fixing this will make it less likely that MTP interruptions will require
a restart to fix. Now the segment-sending loop will exit if it detects
that the session has closed or the current transaction has been canceled.
[mtp] Limit segment buffer size to what USB gadget supports

The 64k buffer size led to a stream of "request length truncated"
warnings from the kernel.
[usb transport] Make sure bulkwrite is finished before starting next one

sendData calls setData in the bulkwriter, which should only be done when
the thread is not running. However it never verifies that the thread
has finished after the previous write. This leads to a race where
sendData may collect the result, then return and be called again before
the writer thread has had a chance to exit.

Fixed by adding a thread wait.
[usb transport] Fix wait-race in bulk reader

It was possible for releaseBuffer to be called before the reader had
started waiting. Fixed by using m_lock to make sure the thread has
started waiting before wakeAll can be called.