From nobody Mon Feb 9 01:30:50 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1518474642126782.0543451435002; Mon, 12 Feb 2018 14:30:42 -0800 (PST) Received: from localhost ([::1]:33211 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1elMcT-0003OB-37 for importer@patchew.org; Mon, 12 Feb 2018 17:30:41 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54127) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1elMaX-0002DZ-0i for qemu-devel@nongnu.org; Mon, 12 Feb 2018 17:28:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1elMaS-0001Gp-0r for qemu-devel@nongnu.org; Mon, 12 Feb 2018 17:28:40 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:33266 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1elMaR-0001FR-Nf for qemu-devel@nongnu.org; Mon, 12 Feb 2018 17:28:35 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D3B2940FB638 for ; Mon, 12 Feb 2018 22:28:30 +0000 (UTC) Received: from gigantic.usersys.redhat.com (dhcp-17-169.bos.redhat.com [10.18.17.169]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9B95DB2894; Mon, 12 Feb 2018 22:28:30 +0000 (UTC) From: Bandan Das To: qemu-devel@nongnu.org Date: Mon, 12 Feb 2018 17:27:05 -0500 Message-Id: <20180212222705.12718-6-bsd@redhat.com> In-Reply-To: <20180212222705.12718-1-bsd@redhat.com> References: <20180212222705.12718-1-bsd@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Mon, 12 Feb 2018 22:28:30 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Mon, 12 Feb 2018 22:28:30 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'bsd@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v2 5/5] usb-mtp: Advertise SendObjectInfo for write support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch implements a dummy ObjectInfo structure so that it's easy to typecast the incoming data. If the metadata is valid, write_pending is set. Also, the incoming filename is utf-16, so, instead of depending on external libraries, just implement a simple function to get the filename Signed-off-by: Bandan Das --- hw/usb/dev-mtp.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 117 insertions(+), 1 deletion(-) diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c index 8d615cabc0..90cf54e2fe 100644 --- a/hw/usb/dev-mtp.c +++ b/hw/usb/dev-mtp.c @@ -47,6 +47,7 @@ enum mtp_code { CMD_GET_OBJECT_INFO =3D 0x1008, CMD_GET_OBJECT =3D 0x1009, CMD_DELETE_OBJECT =3D 0x100b, + CMD_SEND_OBJECT_INFO =3D 0x100c, CMD_SEND_OBJECT =3D 0x100d, CMD_GET_PARTIAL_OBJECT =3D 0x101b, CMD_GET_OBJECT_PROPS_SUPPORTED =3D 0x9801, @@ -66,8 +67,10 @@ enum mtp_code { RES_INVALID_OBJECT_FORMAT_CODE =3D 0x200b, RES_STORE_READ_ONLY =3D 0x200e, RES_PARTIAL_DELETE =3D 0x2012, + RES_STORE_NOT_AVAILABLE =3D 0x2013, RES_SPEC_BY_FORMAT_UNSUPPORTED =3D 0x2014, RES_INVALID_OBJECTINFO =3D 0x2015, + RES_DESTINATION_UNSUPPORTED =3D 0x2020, RES_INVALID_PARENT_OBJECT =3D 0x201a, RES_STORE_FULL =3D 0x200c, RES_INVALID_PARAMETER =3D 0x201d, @@ -195,6 +198,24 @@ struct MTPState { } dataset; }; =20 +/* + * ObjectInfo dataset received from initiator + * Fields we don't care about are ignored + */ +typedef struct { + char __pad1[4]; + uint16_t format; + char __pad2[2]; + uint32_t size; + char __pad3[30]; + uint16_t assoc_type; + uint32_t assoc_desc; + char __pad4[4]; + uint8_t length; + uint16_t filename[0]; + /* string and other data follows */ +} QEMU_PACKED ObjectInfo; + #define TYPE_USB_MTP "usb-mtp" #define USB_MTP(obj) OBJECT_CHECK(MTPState, (obj), TYPE_USB_MTP) =20 @@ -814,6 +835,7 @@ static MTPData *usb_mtp_get_device_info(MTPState *s, MT= PControl *c) CMD_GET_OBJECT_HANDLES, CMD_GET_OBJECT_INFO, CMD_DELETE_OBJECT, + CMD_SEND_OBJECT_INFO, CMD_SEND_OBJECT, CMD_GET_OBJECT, CMD_GET_PARTIAL_OBJECT, @@ -1246,7 +1268,7 @@ static void usb_mtp_object_delete(MTPState *s, uint32= _t handle, static void usb_mtp_command(MTPState *s, MTPControl *c) { MTPData *data_in =3D NULL; - MTPObject *o; + MTPObject *o =3D NULL; uint32_t nres =3D 0, res0 =3D 0; =20 /* sanity checks */ @@ -1393,6 +1415,37 @@ static void usb_mtp_command(MTPState *s, MTPControl = *c) nres =3D 1; res0 =3D data_in->length; break; + case CMD_SEND_OBJECT_INFO: + /* First parameter points to storage id or is 0 */ + if (c->argv[0] && (c->argv[0] !=3D QEMU_STORAGE_ID)) { + usb_mtp_queue_result(s, RES_STORE_NOT_AVAILABLE, c->trans, + 0, 0, 0, 0); + } else if (c->argv[1] && !c->argv[0]) { + /* If second parameter is specified, first must also be specif= ied */ + usb_mtp_queue_result(s, RES_DESTINATION_UNSUPPORTED, c->trans, + 0, 0, 0, 0); + } else { + uint32_t handle =3D c->argv[1]; + if (handle =3D=3D 0xFFFFFFFF || handle =3D=3D 0) { + /* root object */ + o =3D QTAILQ_FIRST(&s->objects); + } else { + o =3D usb_mtp_object_lookup(s, handle); + } + if (o =3D=3D NULL) { + usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, c->tran= s, + 0, 0, 0, 0); + } + if (o->format !=3D FMT_ASSOCIATION) { + usb_mtp_queue_result(s, RES_INVALID_PARENT_OBJECT, c->tran= s, + 0, 0, 0, 0); + } + } + if (o) { + s->dataset.parent_handle =3D o->handle; + } + s->data_out =3D usb_mtp_data_alloc(c); + return; case CMD_SEND_OBJECT: if (!s->write_pending) { usb_mtp_queue_result(s, RES_INVALID_OBJECTINFO, @@ -1502,6 +1555,17 @@ mode_t getumask(void) return mask; } =20 +static void utf16_to_str(uint8_t len, uint16_t *arr, char *name) +{ + int count; + + for (count =3D 0; count < len; count++) { + /* Check for valid ascii */ + assert(!(arr[count] & 0xFF80)); + name[count] =3D arr[count]; + } +} + static void usb_mtp_write_data(MTPState *s) { MTPData *d =3D s->data_out; @@ -1575,6 +1639,45 @@ free: s->write_pending =3D false; } =20 +static void usb_mtp_write_metadata(MTPState *s) +{ + MTPData *d =3D s->data_out; + ObjectInfo *dataset =3D (ObjectInfo *)d->data; + char *filename =3D g_new0(char, dataset->length); + MTPObject *o; + MTPObject *p =3D usb_mtp_object_lookup(s, s->dataset.parent_handle); + uint32_t next_handle =3D s->next_handle; + + assert(!s->write_pending); + + utf16_to_str(dataset->length, dataset->filename, filename); + + o =3D usb_mtp_object_lookup_name(p, filename, dataset->length); + if (o !=3D NULL) { + next_handle =3D o->handle; + } + + s->dataset.filename =3D filename; + s->dataset.format =3D dataset->format; + s->dataset.size =3D dataset->size; + s->dataset.filename =3D filename; + s->write_pending =3D true; + + if (s->dataset.format =3D=3D FMT_ASSOCIATION) { + usb_mtp_write_data(s); + /* next_handle will be allocated to the newly created dir */ + if (d->fd =3D=3D -1) { + usb_mtp_queue_result(s, RES_STORE_FULL, d->trans, + 0, 0, 0, 0); + return; + } + d->fd =3D -1; + } + + usb_mtp_queue_result(s, RES_OK, d->trans, 3, QEMU_STORAGE_ID, + s->dataset.parent_handle, next_handle); +} + static void usb_mtp_get_data(MTPState *s, mtp_container *container, USBPacket *p) { @@ -1599,6 +1702,19 @@ static void usb_mtp_get_data(MTPState *s, mtp_contai= ner *container, } =20 switch (d->code) { + case CMD_SEND_OBJECT_INFO: + usb_packet_copy(p, d->data + d->offset, dlen); + d->offset +=3D dlen; + if (d->offset =3D=3D d->length) { + /* The operation might have already failed */ + if (!s->result) { + usb_mtp_write_metadata(s); + } + usb_mtp_data_free(s->data_out); + s->data_out =3D NULL; + return; + } + break; case CMD_SEND_OBJECT: usb_packet_copy(p, d->data + d->offset, dlen); d->offset +=3D dlen; --=20 2.14.3