usb: gadget: ccid: add support for USB CCID Gadget Device
Subsystems (2)
PATCH v3 1/3 usb: gadget: ccid: add support for USB CCID Gadget Device
Chip Card Interface Device (CCID) protocol is a USB protocol that allows a smartcard device to be connected to a computer via a card reader using a standard USB interface, without the need for each manufacturer of smartcards to provide its own reader or protocol.
This gadget driver makes Linux show up as a CCID device to the host and let a userspace daemon act as the smartcard.
This is useful when the Linux gadget itself should act as a cryptographic device or forward APDUs to an embedded smartcard device.
---
Notes:
v3:
- fix sparse warnings reported by kbuild test robot
v2:
- add the missing changelog text
drivers/usb/gadget/Kconfig | 17 + drivers/usb/gadget/function/Makefile | 1 + drivers/usb/gadget/function/f_ccid.c | 993 +++++++++++++++++++++++++++++++++++ drivers/usb/gadget/function/f_ccid.h | 91 ++++ include/uapi/linux/usb/ccid.h | 93 ++++ 5 files changed, 1195 insertions(+) create mode 100644 drivers/usb/gadget/function/f_ccid.c create mode 100644 drivers/usb/gadget/function/f_ccid.h create mode 100644 include/uapi/linux/usb/ccid.h
drivers/usb/gadget/Kconfig +17 -0
@@ -149,6 +149,9 @@ config USB_LIBCOMPOSITE config USB_F_ACM tristate +config USB_F_CCID + tristate + config USB_F_SS_LB tristate
@@ -248,6 +251,20 @@ config USB_CONFIGFS_ACM ACM serial link. This function can be used to interoperate with MS-Windows hosts or with the Linux-USB "cdc-acm" driver. +config USB_CONFIGFS_CCID + bool "Chip Card Interface Device (CCID)" + depends on USB_CONFIGFS + select USB_F_CCID + help + The CCID function driver provides generic emulation of a + Chip Card Interface Device (CCID). + + You will need a user space server talking to /dev/ccidg*, + since the kernel itself does not implement CCID/TPDU/APDU + protocol. + + For more information, see Documentation/usb/gadget_ccid.rst. + config USB_CONFIGFS_OBEX bool "Object Exchange Model (CDC OBEX)" depends on USB_CONFIGFS
drivers/usb/gadget/function/Makefile +1 -0
@@ -9,6 +9,7 @@ ccflags-y += -I$(srctree)/drivers/usb/gadget/udc/ # USB Functions usb_f_acm-y := f_acm.o obj-$(CONFIG_USB_F_ACM) += usb_f_acm.o +obj-$(CONFIG_USB_F_CCID) += f_ccid.o usb_f_ss_lb-y := f_loopback.o f_sourcesink.o obj-$(CONFIG_USB_F_SS_LB) += usb_f_ss_lb.o obj-$(CONFIG_USB_U_SERIAL) += u_serial.o
drivers/usb/gadget/function/f_ccid.c new file +993 -0
993 lines not on this page · raw message
drivers/usb/gadget/function/f_ccid.h new file +91 -0
91 lines not on this page · raw message
include/uapi/linux/usb/ccid.h new file +93 -0
93 lines not on this page · raw message
PATCH v3 2/3 Documentation: usb: add documentation for USB CCID Gadget Device
Add documentation to give a brief description on how to use the CCID Gadget Device.
This includes a description for all attributes followed by an example on how to setup the device with ConfigFS.
---
Notes:
v3:
- correct the grammer (thanks Randy)
v2:
- add the missing changelog text
Documentation/usb/gadget_ccid.rst | 267 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 Documentation/usb/gadget_ccid.rst
Documentation/usb/gadget_ccid.rst new file +267 -0
@@ -0,0 +1,267 @@ +.. SPDX-License-Identifier: GPL-2.0 + +============ +CCID Gadget +============ + +:Author: Marcus Folkesson <[email protected]> + +Introduction +============ + +The CCID Gadget will present itself as a CCID device to the host system. +The device supports two endpoints for now; BULK IN and BULK OUT. +These endpoints are exposed to userspace via /dev/ccidg*. + +All CCID commands are sent on the BULK-OUT endpoint. Each command sent to the CCID +has an associated ending response. Some commands can also have intermediate +responses. The response is sent on the BULK-IN endpoint. +See Figure 3-3 in the CCID Specification [1]_ for more details. + +The CCID commands must be handled in userspace since the driver is only working +as a transport layer for the TPDUs. + + +CCID Commands +-------------- + +All CCID commands begins with a 10-byte header followed by an optional +data field depending on message type. + ++--------+--------------+-------+----------------------------------+ +| Offset | Field | Size | Description | ++========+==============+=======+==================================+ +| 0 | bMessageType | 1 | Type of message | ++--------+--------------+-------+----------------------------------+ +| 1 | dwLength | 4 | Message specific data length | +| | | | | ++--------+--------------+-------+----------------------------------+ +| 5 | bSlot | 1 | Identifies the slot number | +| | | | for this command | ++--------+--------------+-------+----------------------------------+ +| 6 | bSeq | 1 | Sequence number for command | ++--------+--------------+-------+----------------------------------+ +| 7 | ... | 3 | Fields depends on message type | ++--------+--------------+-------+----------------------------------+ +| 10 | abData | array | Message specific data (OPTIONAL) | ++--------+--------------+-------+----------------------------------+ + + +Multiple CCID gadgets +---------------------- + +It is possible to create multiple instances of the CCID gadget, however, +a much more flexible way is to create one gadget and set the `nslots` attribute +to the number of desired CCID devices. + +All CCID commands specify which slot is the receiver in the `bSlot` field +of the CCID header. + +Usage +===== + +Access from userspace +---------------------- +All communication is by read(2) and write(2) to the corresponding /dev/ccidg* device. +Only one file descriptor is allowed to be open to the device at a time. + +The buffer size provided to read(2) **must be at least** 522 (10 bytes header + 512 bytes payload) +bytes as we are working with whole commands. + +The buffer size provided to write(2) **may not exceed** 522 (10 bytes header + 512 bytes payload) +bytes as we are working with whole commands. + + +Configuration with configfs +---------------------------- + +ConfigFS is used to create and configure the CCID gadget. +In order to get a device to work as intended, a few attributes must +be considered. + +The attributes are described below followed by an example. + +features +~~~~~~~~~ + +The `feature` attribute writes to the dwFeatures field in the class descriptor. +See Table 5.1-1 Smart Card Device Descriptors in the CCID Specification [1]_. + +The value indicates what intelligent features the CCID has. +These values are available to user application as defined in ccid.h [2]_. +The default value is 0x00000000. + +The value is a bitwise OR operation performed on the following values: + ++------------+----------------------------------------------------------------+ +| Value | Description | ++============+================================================================+ +| 0x00000000 | No special characteristics | ++------------+----------------------------------------------------------------+ +| 0x00000002 | Automatic parameter configuration based on ATR data | ++------------+----------------------------------------------------------------+ +| 0x00000004 | Automatic activation of ICC on inserting | ++------------+----------------------------------------------------------------+ +| 0x00000008 | Automatic ICC voltage selection | ++------------+----------------------------------------------------------------+ +| 0x00000010 | Automatic ICC clock frequency change according to active | +| | parameters provided by the Host or self determined | ++------------+----------------------------------------------------------------+ +| 0x00000020 | Automatic baud rate change according to active | +| | parameters provided by the Host or self determined | ++------------+----------------------------------------------------------------+ +| 0x00000040 | Automatic parameters negotiation made by the CCID | ++------------+----------------------------------------------------------------+ +| 0x00000080 | Automatic PPS made by the CCID according to the | +| | active parameters | ++------------+----------------------------------------------------------------+ +| 0x00000100 | CCID can set ICC in clock stop mode | ++------------+----------------------------------------------------------------+ +| 0x00000200 | NAD value other than 00 accepted (T=1 protocol in use) | ++------------+----------------------------------------------------------------+ +| 0x00000400 | Automatic IFSD exchange as first exchange | ++------------+----------------------------------------------------------------+ + + +Only one of the following values may be present to select a level of exchange: + ++------------+--------------------------------------------------+ +| Value | Description | ++============+==================================================+ +| 0x00010000 | TPDU level exchanges with CCID | ++------------+--------------------------------------------------+ +| 0x00020000 | Short APDU level exchange with CCID | ++------------+--------------------------------------------------+ +| 0x00040000 | Short and Extended APDU level exchange with CCID | ++------------+--------------------------------------------------+ + +If none of those values is indicated the level of exchange is +character. + + +protocols +~~~~~~~~~~ +The `protocols` attribute writes to the dwProtocols field in the class descriptor. +See Table 5.1-1 Smart Card Device Descriptors in the CCID Specification [1]_. + +The value is a bitwise OR operation performed on the following values: + ++--------+--------------+ +| Value | Description | ++========+==============+ +| 0x0001 | Protocol T=0 | ++--------+--------------+ +| 0x0002 | Protocol T=1 | ++--------+--------------+ + +If no protocol is selected both T=0 and T=1 will be supported (`protocols` = 0x0003). + +nslots +~~~~~~ + +The `nslots` attribute writes to the bMaxSlotIndex field in the class descriptor. +See Table 5.1-1 Smart Card Device Descriptors in the CCID Specification [1]_. + +This is the index of the highest available slot on this device. All slots are consecutive starting at 00h. +i.e. 0Fh = 16 slots on this device numbered 00h to 0Fh. + +The default value is 0, which means one slot. + + +pinsupport +~~~~~~~~~~~~ + +This value indicates what PIN support features the CCID has. + +The `pinsupport` attribute writes to the dwPINSupport field in the class descriptor. +See Table 5.1-1 Smart Card Device Descriptors in the CCID Specification [1]_. + + +The value is a bitwise OR operation performed on the following values: + ++--------+----------------------------+ +| Value | Description | ++========+============================+ +| 0x00 | No PIN support | ++--------+----------------------------+ +| 0x01 | PIN Verification supported | ++--------+----------------------------+ +| 0x02 | PIN Modification supported | ++--------+----------------------------+ + +The default value is set to 0x00. + + +lcdlayout +~~~~~~~~~~ + +Number of lines and characters for the LCD display used to send messages for PIN entry. + +The `lcdLayout` attribute writes to the wLcdLayout field in the class descriptor. +See Table 5.1-1 Smart Card Device Descriptors in the CCID Specification [1]_. + + +The value is set as follows: + ++--------+------------------------------------+ +| Value | Description | ++========+====================================+ +| 0x0000 | No LCD | ++--------+------------------------------------+ +| 0xXXYY | XX: number of lines | +| | YY: number of characters per line. | ++--------+------------------------------------+ + +The default value is set to 0x0000. + + +Example +------- + +Here is an example on how to setup a CCID gadget with configfs :: + + #!/bin/sh + + CONFIGDIR=/sys/kernel/config + GADGET=$CONFIGDIR/usb_gadget/g0 + FUNCTION=$GADGET/functions/ccid.sc0 + + VID=YOUR_VENDOR_ID_HERE + PID=YOUR_PRODUCT_ID_HERE + UDC=YOUR_UDC_HERE + + #Mount filesystem + mount none -t configfs $CONFIGDIR + + #Populate ID:s + echo $VID > $GADGET/idVendor + echo $PID > $GADGET/idProduct + + #Create and configure the gadget + mkdir $FUNCTION + echo 0x000407B8 > $FUNCTION/features + echo 0x02 > $FUNCTION/protocols + + #Create our english strings + mkdir $GADGET/strings/0x409 + echo 556677 > $GADGET/strings/0x409/serialnumber + echo "Hungry Penguins" > $GADGET/strings/0x409/manufacturer + echo "Harpoon With SmartCard" > $GADGET/strings/0x409/product + + #Create configuration + mkdir $GADGET/configs/c.1 + mkdir $GADGET/configs/c.1/strings/0x409 + echo Config1 > $GADGET/configs/c.1/strings/0x409/configuration + + #Use `Config1` for our CCID gadget + ln -s $FUNCTION $GADGET/configs/c.1 + + #Execute + echo $UDC > $GADGET/UDC + + +References +========== + +.. [1] http://www.usb.org/developers/docs/devclass_docs/DWG_Smart-Card_CCID_Rev110.pdf +.. [2] include/uapi/linux/usb/ccid.h
Re: [PATCH v3 2/3] Documentation: usb: add documentation for USB CCID Gadget Device
Quoted 36 lines · Marcus Folkesson
Add documentation to give a brief description on how to use the CCID Gadget Device.
This includes a description for all attributes followed by an example on how to setup the device with ConfigFS.Signed-off-by Marcus Folkesson <[email protected]>---
Notes:
v3:
- correct the grammer (thanks Randy)
v2:
- add the missing changelog textDocumentation/usb/gadget_ccid.rst | 267 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 Documentation/usb/gadget_ccid.rstDocumentation/usb/gadget_ccid.rst new file +12
-0@@ -0,0 +1,267 @@ +.. SPDX-License-Identifier: GPL-2.0 + +============ +CCID Gadget +============ + +:Author: Marcus Folkesson <[email protected]> + +Introduction +============ + +The CCID Gadget will present itself as a CCID device to the host system.
Documentation/usb/gadget_ccid.rst line 22
Quoted 10 lines
Documentation/usb/gadget_ccid.rst new file +10
-0+The device supports two endpoints for now; BULK IN and BULK OUT. +These endpoints are exposed to userspace via /dev/ccidg*. + +All CCID commands are sent on the BULK-OUT endpoint. Each command sent to the CCID +has an associated ending response. Some commands can also have intermediate +responses. The response is sent on the BULK-IN endpoint. +See Figure 3-3 in the CCID Specification [1]_ for more details. + +The CCID commands must be handled in userspace since the driver is only working +as a transport layer for the TPDUs.
I think that it would be helpful to tell us what the naming of the /dev/ccidg* endpoints looks like. Also, how to distinguish the BULK-IN from the BULK-OUT endpoint.
Quoted 34 lines
Documentation/usb/gadget_ccid.rst new file +34
-0+ + +CCID Commands +-------------- + +All CCID commands begins with a 10-byte header followed by an optional +data field depending on message type. + ++--------+--------------+-------+----------------------------------+ +| Offset | Field | Size | Description | ++========+==============+=======+==================================+ +| 0 | bMessageType | 1 | Type of message | ++--------+--------------+-------+----------------------------------+ +| 1 | dwLength | 4 | Message specific data length | +| | | | | ++--------+--------------+-------+----------------------------------+ +| 5 | bSlot | 1 | Identifies the slot number | +| | | | for this command | ++--------+--------------+-------+----------------------------------+ +| 6 | bSeq | 1 | Sequence number for command | ++--------+--------------+-------+----------------------------------+ +| 7 | ... | 3 | Fields depends on message type | ++--------+--------------+-------+----------------------------------+ +| 10 | abData | array | Message specific data (OPTIONAL) | ++--------+--------------+-------+----------------------------------+ + + +Multiple CCID gadgets +---------------------- + +It is possible to create multiple instances of the CCID gadget, however, +a much more flexible way is to create one gadget and set the `nslots` attribute +to the number of desired CCID devices. +
Documentation/usb/gadget_ccid.rst line 66
Quoted 10 lines
Documentation/usb/gadget_ccid.rst new file +10
-0+All CCID commands specify which slot is the receiver in the `bSlot` field +of the CCID header. + +Usage +===== + +Access from userspace +---------------------- +All communication is by read(2) and write(2) to the corresponding /dev/ccidg* device. +Only one file descriptor is allowed to be open to the device at a time.
thanks,
Re: [PATCH v3 2/3] Documentation: usb: add documentation for USB CCID Gadget Device
Hi Randy,
Quoted 43 lines · Randy Dunlap
Quoted 42 lines · Marcus Folkesson
Add documentation to give a brief description on how to use the CCID Gadget Device.
This includes a description for all attributes followed by an example on how to setup the device with ConfigFS.Signed-off-by Marcus Folkesson <[email protected]>---
Notes:
v3:
- correct the grammer (thanks Randy)
v2:
- add the missing changelog textDocumentation/usb/gadget_ccid.rst | 267 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 Documentation/usb/gadget_ccid.rstDocumentation/usb/gadget_ccid.rst new file +18
-0@@ -0,0 +1,267 @@ +.. SPDX-License-Identifier: GPL-2.0 + +============ +CCID Gadget +============ + +:Author: Marcus Folkesson <[email protected]> + +Introduction +============ + +The CCID Gadget will present itself as a CCID device to the host system. +The device supports two endpoints for now; BULK IN and BULK OUT. +These endpoints are exposed to userspace via /dev/ccidg*. + +All CCID commands are sent on the BULK-OUT endpoint. Each command sent to the CCID +has an associated ending response. Some commands can also have intermediate +responses. The response is sent on the BULK-IN endpoint.
Quoted 8 lines
Documentation/usb/gadget_ccid.rst line 22
Quoted 4 lines
Documentation/usb/gadget_ccid.rst new file +4
-0+See Figure 3-3 in the CCID Specification [1]_ for more details. + +The CCID commands must be handled in userspace since the driver is only working +as a transport layer for the TPDUs.I think that it would be helpful to tell us what the naming of the
/dev/ccidg*endpoints looks like. Also, how to distinguish the BULK-IN from the BULK-OUT endpoint.
You are right, it is not clear.
The devices are named as /dev/ccidg[0-4] and is per device. Writing/reading to the device is using BULK-IN/BULK-OUT, the /dev/ccidg* device is not bound to a particular endpoint.
I will come up with something that make it more clear.
Thank you!
Quoted 50 lines
Quoted 35 lines
Documentation/usb/gadget_ccid.rst new file +34
-0+ + +CCID Commands +-------------- + +All CCID commands begins with a 10-byte header followed by an optional +data field depending on message type. + ++--------+--------------+-------+----------------------------------+ +| Offset | Field | Size | Description | ++========+==============+=======+==================================+ +| 0 | bMessageType | 1 | Type of message | ++--------+--------------+-------+----------------------------------+ +| 1 | dwLength | 4 | Message specific data length | +| | | | | ++--------+--------------+-------+----------------------------------+ +| 5 | bSlot | 1 | Identifies the slot number | +| | | | for this command | ++--------+--------------+-------+----------------------------------+ +| 6 | bSeq | 1 | Sequence number for command | ++--------+--------------+-------+----------------------------------+ +| 7 | ... | 3 | Fields depends on message type | ++--------+--------------+-------+----------------------------------+ +| 10 | abData | array | Message specific data (OPTIONAL) | ++--------+--------------+-------+----------------------------------+ + + +Multiple CCID gadgets +---------------------- + +It is possible to create multiple instances of the CCID gadget, however, +a much more flexible way is to create one gadget and set the `nslots` attribute +to the number of desired CCID devices. +Documentation/usb/gadget_ccid.rst line 66
Quoted 10 lines
Documentation/usb/gadget_ccid.rst new file +10
-0+All CCID commands specify which slot is the receiver in the `bSlot` field +of the CCID header. + +Usage +===== + +Access from userspace +---------------------- +All communication is by read(2) and write(2) to the corresponding /dev/ccidg* device. +Only one file descriptor is allowed to be open to the device at a time.Reviewed-by Randy Dunlap <[email protected]>
Quoted 3 lines
thanks,
Signature · 1 line
~Randy
Best regards,
Marcus Folkesson
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v3 3/3 MAINTAINERS: add USB CCID Gadget Device
In reply to Marcus FolkessonAdd MAINTAINERS entry for USB CCID Gadget Device
---
Notes:
v3:
- No changes
v2:
- No changes
MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
@@ -14541,6 +14541,14 @@ L: [email protected] S: Maintained F: drivers/usb/storage/uas.c +USB CCID GADGET +M: Marcus Folkesson <[email protected]> +L: [email protected] +S: Maintained +F: drivers/usb/gadget/function/f_ccid.* +F: include/uapi/linux/usb/ccid.h +F: Documentation/usb/gadget_ccid.rst + USB CDC ETHERNET DRIVER M: Oliver Neukum <[email protected]> L: [email protected]
Quoted 8 lines · Marcus Folkesson
Chip Card Interface Device (CCID) protocol is a USB protocol that allows a smartcard device to be connected to a computer via a card reader using a standard USB interface, without the need for each manufacturer of smartcards to provide its own reader or protocol.
This gadget driver makes Linux show up as a CCID device to the host and let a userspace daemon act as the smartcard.
Quoted 6 lines
This is useful when the Linux gadget itself should act as a cryptographic device or forward APDUs to an embedded smartcard device.
+ * Copyright (C) 2018 Marcus Folkesson <[email protected]>
+ *
Redundant line
Quoted 1 line
+static
DEFINE_IDA(ccidg_ida);
Where is it destroyed?
drivers/usb/gadget/function/f_ccid.c line 191
Quoted 2 lines
drivers/usb/gadget/function/f_ccid.c new file +2
-0+ ccidg_class =NULL ; +return PTR_ERR (ccidg_class);
Are you sure?
drivers/usb/gadget/function/f_ccid.c line 311
Quoted 2 lines
drivers/usb/gadget/function/f_ccid.c new file +2
-0+if (!list_empty (head)) { + req = list_first_entry(head, struct usb_request, list);
list_first_entry_or_null()
Quoted 1 line
+
req->length= len;
Perhaps assign this obly if malloc successedeed ?
drivers/usb/gadget/function/f_ccid.c line 388
Quoted 3 lines
drivers/usb/gadget/function/f_ccid.c new file +2
-0+ req->buf =kmalloc (len,GFP_ATOMIC ); +if (req->buf ==NULL ) {
if (!req->buf) ?
drivers/usb/gadget/function/f_ccid.c line 398
Quoted 8 lines
drivers/usb/gadget/function/f_ccid.c new file +6
-0+usb_ep_free_request (ep, req); +return ERR_PTR (-ENOMEM ); + } +static void ccidg_request_free (struct usb_request *req,struct usb_ep *ep) +{ +if (req) {
Is it even possible?
What about
if (!req)
return;
?
drivers/usb/gadget/function/f_ccid.c line 426
Quoted 6 lines
drivers/usb/gadget/function/f_ccid.c new file +5
-0+kfree (req->buf); +usb_ep_free_request (ep, req); + } +} + *(__le32 *) req->buf = ccid_class_desc.dwDefaultClock;
Hmm... put_unaligned()? cpu_to_le32()? cpu_to_le32p()?
Quoted 1 line
+ *(__le32 *)
req->buf= ccid_class_desc.dwDataRate;
Ditto.
Review
Quoted 2 lines
(quoted diff) +2
-0+ } + }
Indentation.
drivers/usb/gadget/function/f_ccid.c line 448
Quoted 2 lines
drivers/usb/gadget/function/f_ccid.c new file +2
-0+/* responded with data transfer or status phase? */ +if (ret >=0 ) {
Why not
if (ret < 0)
return ret;
?
drivers/usb/gadget/function/f_ccid.c line 558
Quoted 7 lines
drivers/usb/gadget/function/f_ccid.c new file +6
-0+ } + +return ret; +} +atomic_set (&ccidg->online,1 ); +return ret;
return 0; ?
drivers/usb/gadget/function/f_ccid.c line 575
Quoted 3 lines
drivers/usb/gadget/function/f_ccid.c new file +2
-0+struct f_ccidg *ccidg; + ccidg = container_of(inode->i_cdev, struct f_ccidg, cdev);
One line ?
Quoted 1 line
+ xfer = (
req->actual< count) ?req->actual: count;
min_t()
drivers/usb/gadget/function/f_ccid.c line 702
Quoted 5 lines
drivers/usb/gadget/function/f_ccid.c new file +5
-0+ ret =wait_event_interruptible (bulk_dev->write_wq, + ((req =ccidg_req_get (ccidg, &bulk_dev->tx_idle)))); + +if (ret <0 ) +return -ERESTARTSYS ;
Redundant blank line above.
drivers/usb/gadget/function/f_ccid.c line 776
Quoted 6 lines
drivers/usb/gadget/function/f_ccid.c new file +4
-0+static void ccidg_function_free (struct usb_function *f) +{ +struct f_ccidg_opts *opts; + opts = container_of(f->fi, struct f_ccidg_opts, func_inst);
One line.
drivers/usb/gadget/function/f_ccid.c line 780
Quoted 2 lines
drivers/usb/gadget/function/f_ccid.c new file +2
-0+mutex_lock (&opts->lock); + --opts->refcnt;
-- will work
drivers/usb/gadget/function/f_ccid.c line 908
Quoted 6 lines
drivers/usb/gadget/function/f_ccid.c new file +4
-0+mutex_unlock (&opts->lock); +} +struct f_ccidg_opts *opts; + opts = container_of(fi, struct f_ccidg_opts, func_inst);
Perhaps one line ?
Quoted 1 line
+ ++opts->refcnt;
X++ would work as well.
drivers/usb/gadget/function/f_ccid.c line 935
Quoted 3 lines
drivers/usb/gadget/function/f_ccid.c new file +3
-0+struct f_ccidg_opts *opts; + + opts = container_of(f, struct f_ccidg_opts, func_inst);
Perhaps one line?
Quoted 1 line
+#define
CCID_PINSUPOORT_NONE0x00
(0 << 0)
?
for sake of consistency
Quoted 2 lines
include/uapi/linux/usb/ccid.h new file +2
-0+#define CCID_PINSUPOORT_VERIFICATION (1 << 1) +#define CCID_PINSUPOORT_MODIFICATION (1 << 2)
Hi Andy,
Thank you for your comments!
Many good catches here!
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Marcus Folkesson
Chip Card Interface Device (CCID) protocol is a USB protocol that allows a smartcard device to be connected to a computer via a card reader using a standard USB interface, without the need for each manufacturer of smartcards to provide its own reader or protocol.
This gadget driver makes Linux show up as a CCID device to the host and let a userspace daemon act as the smartcard.
Quoted 9 lines
Quoted 6 lines
This is useful when the Linux gadget itself should act as a cryptographic device or forward APDUs to an embedded smartcard device.
+ * Copyright (C) 2018 Marcus Folkesson <[email protected]>
+ *
Redundant line
Yep
Quoted 3 lines
Quoted 1 line
+static
DEFINE_IDA(ccidg_ida);Where is it destroyed?
Hm, I'm not sure it needs to be destroyed. From lib/idr.c:
* You can also use ida_get_new_above() if you need an ID to be allocated
* above a particular number. ida_destroy() can be used to dispose of an
* IDA without needing to free the individual IDs in it. You can use
* ida_is_empty() to find out whether the IDA has any IDs currently allocated.
An empty ccidg_ida is the indication that we should clean up our mess:
static void ccidg_free_inst(struct usb_function_instance *f)
...
if (ida_is_empty(&ccidg_ida)) ccidg_cleanup();
If the IDA is empty, should I call ida_destroy() anyway? Other similiar drivers does not seems to do that.
I must say that I'm not very familiar with the IDA API.
Quoted 5 lines
drivers/usb/gadget/function/f_ccid.c line 191
Quoted 3 lines
drivers/usb/gadget/function/f_ccid.c new file +2
-0+ ccidg_class =NULL ; +return PTR_ERR (ccidg_class);Are you sure?
Heh, :-)
Quoted 5 lines
drivers/usb/gadget/function/f_ccid.c line 311
Quoted 3 lines
drivers/usb/gadget/function/f_ccid.c new file +2
-0+if (!list_empty (head)) { + req = list_first_entry(head, struct usb_request, list);
list_first_entry_or_null()
Will do, thanks.
Quoted 4 lines
Quoted 2 lines
+
req->length= len;Perhaps assign this obly if malloc successedeed ?
Will do.
Quoted 6 lines
drivers/usb/gadget/function/f_ccid.c line 388
Quoted 4 lines
drivers/usb/gadget/function/f_ccid.c new file +2
-0+ req->buf =kmalloc (len,GFP_ATOMIC ); +if (req->buf ==NULL ) {if (!req->buf) ?
Will do
Quoted 10 lines
drivers/usb/gadget/function/f_ccid.c line 398
Quoted 9 lines
drivers/usb/gadget/function/f_ccid.c new file +6
-0+usb_ep_free_request (ep, req); +return ERR_PTR (-ENOMEM ); + } +static void ccidg_request_free (struct usb_request *req,struct usb_ep *ep) +{ +if (req) {
Quoted 8 lines
Is it even possible?
What about
if (!req)
return;?
Hmm, maybe it is not.
I think I remove this check.
Quoted 10 lines
drivers/usb/gadget/function/f_ccid.c line 426
Quoted 7 lines
drivers/usb/gadget/function/f_ccid.c new file +5
-0+kfree (req->buf); +usb_ep_free_request (ep, req); + } +} + *(__le32 *) req->buf = ccid_class_desc.dwDefaultClock;Hmm...
put_unaligned()?cpu_to_le32()?cpu_to_le32p()?
Hmm, dwDefaultClock is already represented in little endian format. If I use any of those functions, would not the byte order be swaped on a big endian system?
dwDefaultClock is set as:
.dwDefaultClock = cpu_to_le32(3580),
I'm not sure what the best practice is here.
Quoted 8 lines
Quoted 1 line
+ *(__le32 *)
req->buf= ccid_class_desc.dwDataRate;Ditto.
Review
Quoted 2 lines
(quoted diff) +2
-0+ } + }Indentation.
I remove the extra brackets from the case instead.
Quoted 11 lines
drivers/usb/gadget/function/f_ccid.c line 448
Quoted 3 lines
drivers/usb/gadget/function/f_ccid.c new file +2
-0+/* responded with data transfer or status phase? */ +if (ret >=0 ) {Why not
if (ret < 0)
return ret;?
Will do
Quoted 9 lines
drivers/usb/gadget/function/f_ccid.c line 558
Quoted 7 lines
drivers/usb/gadget/function/f_ccid.c new file +6
-0+ } + +return ret; +} +atomic_set (&ccidg->online,1 ); +return ret;return 0; ?
Will do
Quoted 6 lines
drivers/usb/gadget/function/f_ccid.c line 575
Quoted 4 lines
drivers/usb/gadget/function/f_ccid.c new file +2
-0+struct f_ccidg *ccidg; + ccidg = container_of(inode->i_cdev, struct f_ccidg, cdev);One line ?
The line exceeds 80 characters then, but I will put it like this:
struct f_ccidg *ccidg = container_of(inode->i_cdev, struct f_ccidg, cdev);
Quoted 5 lines
Quoted 2 lines
+ xfer = (
req->actual< count) ?req->actual: count;
min_t()
Thanks, will do
Quoted 8 lines
drivers/usb/gadget/function/f_ccid.c line 702
Quoted 5 lines
drivers/usb/gadget/function/f_ccid.c new file +5
-0+ ret =wait_event_interruptible (bulk_dev->write_wq, + ((req =ccidg_req_get (ccidg, &bulk_dev->tx_idle)))); + +if (ret <0 ) +return -ERESTARTSYS ;Redundant blank line above.
I remove the extra blank line
Quoted 8 lines
drivers/usb/gadget/function/f_ccid.c line 776
Quoted 6 lines
drivers/usb/gadget/function/f_ccid.c new file +4
-0+static void ccidg_function_free (struct usb_function *f) +{ +struct f_ccidg_opts *opts; + opts = container_of(f->fi, struct f_ccidg_opts, func_inst);One line.
See above
Quoted 6 lines
drivers/usb/gadget/function/f_ccid.c line 780
Quoted 4 lines
drivers/usb/gadget/function/f_ccid.c new file +2
-0+mutex_lock (&opts->lock); + --opts->refcnt;-- will work
Will do
Quoted 9 lines
drivers/usb/gadget/function/f_ccid.c line 908
Quoted 7 lines
drivers/usb/gadget/function/f_ccid.c new file +4
-0+mutex_unlock (&opts->lock); +} +struct f_ccidg_opts *opts; + opts = container_of(fi, struct f_ccidg_opts, func_inst);Perhaps one line ?
See above
Quoted 2 lines
Quoted 1 line
+ ++opts->refcnt;
X++ would work as well.
Will do
Quoted 6 lines
drivers/usb/gadget/function/f_ccid.c line 935
Quoted 3 lines
drivers/usb/gadget/function/f_ccid.c new file +3
-0+struct f_ccidg_opts *opts; + + opts = container_of(f, struct f_ccidg_opts, func_inst);Perhaps one line?
See above
Quoted 7 lines
Quoted 1 line
+#define
CCID_PINSUPOORT_NONE0x00(0 << 0)
?
for sake of consistency
Yep, will change
Quoted 7 lines
Quoted 3 lines
include/uapi/linux/usb/ccid.h new file +2
-0+#define CCID_PINSUPOORT_VERIFICATION (1 << 1) +#define CCID_PINSUPOORT_MODIFICATION (1 << 2)Signature · 2 lines
With Best Regards, Andy Shevchenko
Best regards,
Marcus Folkesson
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Greg Kroah-Hartman
maintainerQuoted 45 lines · Marcus Folkesson
Hi Andy,
Thank you for your comments!
Many good catches here!Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Marcus Folkesson
Chip Card Interface Device (CCID) protocol is a USB protocol that allows a smartcard device to be connected to a computer via a card reader using a standard USB interface, without the need for each manufacturer of smartcards to provide its own reader or protocol.
This gadget driver makes Linux show up as a CCID device to the host and let a userspace daemon act as the smartcard.
Quoted 9 lines
Quoted 6 lines
This is useful when the Linux gadget itself should act as a cryptographic device or forward APDUs to an embedded smartcard device.
+ * Copyright (C) 2018 Marcus Folkesson <[email protected]>
+ *
Redundant line
Yep
Quoted 3 lines
Quoted 1 line
+static
DEFINE_IDA(ccidg_ida);Where is it destroyed?
Hm, I'm not sure it needs to be destroyed. From
lib/idr.c:* You can also use
ida_get_new_above()if you need an ID to be allocated
* above a particular number.ida_destroy()can be used to dispose of an
* IDA without needing to free the individual IDs in it. You can use
*ida_is_empty()to find out whether the IDA has any IDs currently allocated.An empty
ccidg_idais the indication that we should clean up our mess:static void
ccidg_free_inst(struct usb_function_instance *f)
...
Quoted 7 lines
if (ida_is_empty(&ccidg_ida)) ccidg_cleanup();If the IDA is empty, should I call
ida_destroy()anyway? Other similiar drivers does not seems to do that.I must say that I'm not very familiar with the IDA API.
When your module is removed, you need to clean up any remaining memory that the ida used. It's not obvious at all, and is a pain as you would think that if you statically allocate one, like you have here, it would not be needed. You need to just call:
ida_destroy(&ccidg_ida);
in your module exit function.
Hope this helps,
greg k-h
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi Greg,
Quoted 54 lines · Greg Kroah-Hartman
Quoted 45 lines · Marcus Folkesson
Hi Andy,
Thank you for your comments!
Many good catches here!Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Marcus Folkesson
Chip Card Interface Device (CCID) protocol is a USB protocol that allows a smartcard device to be connected to a computer via a card reader using a standard USB interface, without the need for each manufacturer of smartcards to provide its own reader or protocol.
This gadget driver makes Linux show up as a CCID device to the host and let a userspace daemon act as the smartcard.
Quoted 9 lines
Quoted 6 lines
This is useful when the Linux gadget itself should act as a cryptographic device or forward APDUs to an embedded smartcard device.
+ * Copyright (C) 2018 Marcus Folkesson <[email protected]>
+ *
Redundant line
Yep
Quoted 3 lines
Quoted 1 line
+static
DEFINE_IDA(ccidg_ida);Where is it destroyed?
Hm, I'm not sure it needs to be destroyed. From
lib/idr.c:* You can also use
ida_get_new_above()if you need an ID to be allocated
* above a particular number.ida_destroy()can be used to dispose of an
* IDA without needing to free the individual IDs in it. You can use
*ida_is_empty()to find out whether the IDA has any IDs currently allocated.An empty
ccidg_idais the indication that we should clean up our mess:static void
ccidg_free_inst(struct usb_function_instance *f)
...Quoted 7 lines
if (ida_is_empty(&ccidg_ida)) ccidg_cleanup();If the IDA is empty, should I call
ida_destroy()anyway? Other similiar drivers does not seems to do that.I must say that I'm not very familiar with the IDA API.
Quoted 8 lines
When your module is removed, you need to clean up any remaining memory that the ida used. It's not obvious at all, and is a pain as you would think that if you statically allocate one, like you have here, it would not be needed. You need to just call:
ida_destroy(&ccidg_ida);in your module exit function.
Hope this helps,
Thank you for making it clear.
Maybe I should use
#define DECLARE_USB_FUNCTION(_name, _inst_alloc, _func_alloc) \
instead of
#define DECLARE_USB_FUNCTION_INIT(_name, _inst_alloc, _func_alloc) \
and provide my own module_init/module_exit functions then?
Just give me a hint and I will do the same forf_printer.c andf_hid.c
as those use IDAs in a similiar way.
Quoted 2 lines
greg k-h
Best regards,
Marcus Folkesson
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Greg Kroah-Hartman
maintainerQuoted 68 lines · Marcus Folkesson
Hi Greg,
Quoted 54 lines · Greg Kroah-Hartman
Quoted 45 lines · Marcus Folkesson
Hi Andy,
Thank you for your comments!
Many good catches here!Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Marcus Folkesson
Chip Card Interface Device (CCID) protocol is a USB protocol that allows a smartcard device to be connected to a computer via a card reader using a standard USB interface, without the need for each manufacturer of smartcards to provide its own reader or protocol.
This gadget driver makes Linux show up as a CCID device to the host and let a userspace daemon act as the smartcard.
Quoted 9 lines
Quoted 6 lines
This is useful when the Linux gadget itself should act as a cryptographic device or forward APDUs to an embedded smartcard device.
+ * Copyright (C) 2018 Marcus Folkesson <[email protected]>
+ *
Redundant line
Yep
Quoted 3 lines
Quoted 1 line
+static
DEFINE_IDA(ccidg_ida);Where is it destroyed?
Hm, I'm not sure it needs to be destroyed. From
lib/idr.c:* You can also use
ida_get_new_above()if you need an ID to be allocated
* above a particular number.ida_destroy()can be used to dispose of an
* IDA without needing to free the individual IDs in it. You can use
*ida_is_empty()to find out whether the IDA has any IDs currently allocated.An empty
ccidg_idais the indication that we should clean up our mess:static void
ccidg_free_inst(struct usb_function_instance *f)
...Quoted 7 lines
if (ida_is_empty(&ccidg_ida)) ccidg_cleanup();If the IDA is empty, should I call
ida_destroy()anyway? Other similiar drivers does not seems to do that.I must say that I'm not very familiar with the IDA API.
Quoted 8 lines
When your module is removed, you need to clean up any remaining memory that the ida used. It's not obvious at all, and is a pain as you would think that if you statically allocate one, like you have here, it would not be needed. You need to just call:
ida_destroy(&ccidg_ida);in your module exit function.
Hope this helps,
Thank you for making it clear.
Quoted 7 lines
Maybe I should use
#defineDECLARE_USB_FUNCTION(_name, _inst_alloc, _func_alloc)\instead of
#defineDECLARE_USB_FUNCTION_INIT(_name, _inst_alloc, _func_alloc)\and provide my own module_init/module_exit functions then?
Probably, yes.
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 5 lines · Marcus Folkesson
Chip Card Interface Device (CCID) protocol is a USB protocol that allows a smartcard device to be connected to a computer via a card reader using a standard USB interface, without the need for each manufacturer of smartcards to provide its own reader or protocol.
Quoted 7 lines
This gadget driver makes Linux show up as a CCID device to the host and let a userspace daemon act as the smartcard.
This is useful when the Linux gadget itself should act as a cryptographic device or forward APDUs to an embedded smartcard device.
Signed-off-by Marcus Folkesson <[email protected]>
this could be done entirely in userspace with functionfs, why do we need this part in the kernel? It does very little.
Hi Filipe,
Quoted 10 lines · Felipe Balbi
Quoted 8 lines · Marcus Folkesson
Chip Card Interface Device (CCID) protocol is a USB protocol that allows a smartcard device to be connected to a computer via a card reader using a standard USB interface, without the need for each manufacturer of smartcards to provide its own reader or protocol.
This gadget driver makes Linux show up as a CCID device to the host and let a userspace daemon act as the smartcard.
Quoted 7 lines
Quoted 4 lines
This is useful when the Linux gadget itself should act as a cryptographic device or forward APDUs to an embedded smartcard device.
Signed-off-by Marcus Folkesson <[email protected]>this could be done entirely in userspace with functionfs, why do we need this part in the kernel? It does very little.
Andrzej pointed this out, and I actually do not have any good answer more than that the userspace application could be kept small and the important configuration of the CCID device is done with well (I hope) documented configfs attributes.
Quoted 3 lines
Signature · 2 lines
-- balbi
Best regards,
Marcus Folkesson
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi Felipe,
Should I send out v4 or what do you think?
Quoted 27 lines · Marcus Folkesson
Hi Filipe,
Quoted 10 lines · Felipe Balbi
Quoted 8 lines · Marcus Folkesson
Chip Card Interface Device (CCID) protocol is a USB protocol that allows a smartcard device to be connected to a computer via a card reader using a standard USB interface, without the need for each manufacturer of smartcards to provide its own reader or protocol.
This gadget driver makes Linux show up as a CCID device to the host and let a userspace daemon act as the smartcard.
Quoted 7 lines
Quoted 4 lines
This is useful when the Linux gadget itself should act as a cryptographic device or forward APDUs to an embedded smartcard device.
Signed-off-by Marcus Folkesson <[email protected]>this could be done entirely in userspace with functionfs, why do we need this part in the kernel? It does very little.
Andrzej pointed this out, and I actually do not have any good answer more than that the userspace application could be kept small and the important configuration of the CCID device is done with well (I hope) documented configfs attributes.
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
Signature · 1 line
balbiBest regards,
Marcus Folkesson
Thank you,
Marcus Folkesson
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi,
Quoted 3 lines · Marcus Folkesson
Hi Felipe,
Should I send out v4 or what do you think?
sorry for the delay, have been busy with other tasks.
Quoted 19 lines · Marcus Folkesson
Quoted 18 lines
Hi Filipe,
Quoted 15 lines · Felipe Balbi
Quoted 12 lines · Marcus Folkesson
Chip Card Interface Device (CCID) protocol is a USB protocol that allows a smartcard device to be connected to a computer via a card reader using a standard USB interface, without the need for each manufacturer of smartcards to provide its own reader or protocol.
This gadget driver makes Linux show up as a CCID device to the host and let a userspace daemon act as the smartcard.
This is useful when the Linux gadget itself should act as a cryptographic device or forward APDUs to an embedded smartcard device.
Signed-off-by Marcus Folkesson <[email protected]>
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
this could be done entirely in userspace with functionfs, why do we need this part in the kernel? It does very little.
Andrzej pointed this out, and I actually do not have any good answer more than that the userspace application could be kept small and the important configuration of the CCID device is done with well (I hope) documented configfs attributes.
can we use existing open source applications without modification by accepting this glue layer?
Hi Filipe,
Quoted 10 lines · Felipe Balbi
Hi,
Quoted 3 lines · Marcus Folkesson
Hi Felipe,
Should I send out v4 or what do you think?
sorry for the delay, have been busy with other tasks.
It is no hurry :-)
Quoted 22 lines
Quoted 22 lines · Marcus Folkesson
Quoted 21 lines
Hi Filipe,
Quoted 17 lines · Felipe Balbi
Quoted 5 lines · Marcus Folkesson
Chip Card Interface Device (CCID) protocol is a USB protocol that allows a smartcard device to be connected to a computer via a card reader using a standard USB interface, without the need for each manufacturer of smartcards to provide its own reader or protocol.
Quoted 7 lines
This gadget driver makes Linux show up as a CCID device to the host and let a userspace daemon act as the smartcard.
This is useful when the Linux gadget itself should act as a cryptographic device or forward APDUs to an embedded smartcard device.
Signed-off-by Marcus Folkesson <[email protected]>this could be done entirely in userspace with functionfs, why do we need this part in the kernel? It does very little.
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Andrzej pointed this out, and I actually do not have any good answer more than that the userspace application could be kept small and the important configuration of the CCID device is done with well (I hope) documented configfs attributes.
can we use existing open source applications without modification by accepting this glue layer?
If you mean existing open source application to talk to the "daemon", the answer is yes. I have been using PCSC-lite and OpenSC on Linux, and PCSC on Windows to communicate with the smartcard.
If you mean existing application as the "daemon", the answer is probably no.
Quoted 3 lines
Signature · 2 lines
-- balbi
Thanks,
Best regards
Marcus Folkesson
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi Filipe,
Quoted 47 lines · Marcus Folkesson
Hi Filipe,
Quoted 10 lines · Felipe Balbi
Hi,
Quoted 3 lines · Marcus Folkesson
Hi Felipe,
Should I send out v4 or what do you think?
sorry for the delay, have been busy with other tasks.
It is no hurry :-)
Quoted 22 lines
Quoted 22 lines · Marcus Folkesson
Quoted 21 lines
Hi Filipe,
Quoted 17 lines · Felipe Balbi
Quoted 5 lines · Marcus Folkesson
Chip Card Interface Device (CCID) protocol is a USB protocol that allows a smartcard device to be connected to a computer via a card reader using a standard USB interface, without the need for each manufacturer of smartcards to provide its own reader or protocol.
Quoted 7 lines
This gadget driver makes Linux show up as a CCID device to the host and let a userspace daemon act as the smartcard.
This is useful when the Linux gadget itself should act as a cryptographic device or forward APDUs to an embedded smartcard device.
Signed-off-by Marcus Folkesson <[email protected]>this could be done entirely in userspace with functionfs, why do we need this part in the kernel? It does very little.
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Andrzej pointed this out, and I actually do not have any good answer more than that the userspace application could be kept small and the important configuration of the CCID device is done with well (I hope) documented configfs attributes.
can we use existing open source applications without modification by accepting this glue layer?
Quoted 6 lines
If you mean existing open source application to talk to the "daemon", the answer is yes. I have been using PCSC-lite and OpenSC on Linux, and PCSC on Windows to communicate with the smartcard.
If you mean existing application as the "daemon", the answer is probably no.
Just sending a friendly ping on this one.
I've sent out v4 as I had it prepared for a long time, just to catch comments in the meanwhile, but none so far.
Quoted 10 lines
Quoted 4 lines
Signature · 2 lines
-- balbiThanks,
Best regards
Marcus Folkesson
Thanks,
Best regards
Marcus Folkesson
Quoted 1 line
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html