usb: typec: ucsi: add support for separate DP altmode devices
Subsystems (2)
PATCH v5 1/1 usb: typec: ucsi: add support for separate DP altmode devices
From: Ajay Gupta <[email protected]>
CCGx controller used on NVIDIA GPU card has two separate display altmode for two DP pin assignments. UCSI specification doesn't prohibits using separate display altmode.
Current UCSI Type-C framework expects only one display altmode for all DP pin assignment. This patch squashes two separate display altmode into single altmode to support controllers with separate display altmode. We first read all the alternate modes of connector and then run through it to know if there are separate display altmodes. If so, it prepares a new port altmode set after squashing two or more separate altmodes into one.
---
Original discussion on this issue is at [1]
Change from v4->v5
- Fix comments style as per Heikki's review
1. https://marc.info/?l=linux-usb&m=154905866830998&w=2
drivers/usb/typec/ucsi/ucsi.c | 68 +++++++++-- drivers/usb/typec/ucsi/ucsi.h | 8 ++ drivers/usb/typec/ucsi/ucsi_ccg.c | 181 +++++++++++++++++++++++++++++- 3 files changed, 244 insertions(+), 13 deletions(-)
drivers/usb/typec/ucsi/ucsi.c +56 -12
@@ -346,12 +346,17 @@ static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)int max_altmodes =UCSI_MAX_ALTMODES ;struct typec_altmode_desc desc;struct ucsi_altmode alt[2 ]; +struct ucsi_altmode orig[UCSI_MAX_ALTMODES ]; +struct ucsi_altmode updated[UCSI_MAX_ALTMODES ]; +struct ucsi *ucsi = con->ucsi; +bool multi_dp = false;struct ucsi_control ctrl;int num =1 ;int ret;int len;int j;int i; +int k =0 ;if (!(con->ucsi->cap.features &UCSI_CAP_ALT_MODE_DETAILS ))return 0 ;
@@ -362,35 +367,74 @@ static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)if (recipient ==UCSI_RECIPIENT_CON ) max_altmodes = con->ucsi->cap.num_alt_modes; +memset (orig,0 ,sizeof (orig)); +memset (updated,0 ,sizeof (updated)); + +/* First get all the alternate modes */ for (i =0 ; i < max_altmodes;) {memset (alt,0 ,sizeof (alt));UCSI_CMD_GET_ALTERNATE_MODES (ctrl, recipient, con->num, i,1 ); len =ucsi_run_command (con->ucsi, &ctrl, alt,sizeof (alt)); -if (len <=0 ) +/* +* We are collecting all altmodes first and then registering. +* Some type-C device will return zero length data beyond last +* alternate modes. We should not return if length is zero. +*/ +if (len <0 )return len; +/* We got all altmodes, now break out and register them */ +if (!len) +break ; +/* * This code is requesting one alt mode at a time, but some PPMs * may still return two. If that happens both alt modes need be -* registered and the offset for the next alt mode has to be +* saved and the offset for the next alt mode has to be * incremented. */ num = len /sizeof (alt[0 ]); i += num;for (j =0 ; j < num; j++) { -if (!alt[j].svid) -return 0 ; - -memset (&desc,0 ,sizeof (desc)); - desc.vdo = alt[j].mid; - desc.svid = alt[j].svid; - desc.roles =TYPEC_PORT_DRD ; +if (!alt[j].svid) { +/* break out of outer loop and register */ + i = max_altmodes; +break ; + } + + orig[k].mid = alt[j].mid; + orig[k].svid = alt[j].svid; + k++; + } + } - ret =ucsi_register_altmode (con, &desc, recipient); -if (ret) -return ret; +/* +* Update the original altmode table as some ppms may report +* multiple DP altmodes. +*/ +if (recipient ==UCSI_RECIPIENT_CON && ucsi->ppm && + ucsi->ppm->update_altmodes) + multi_dp = ucsi->ppm->update_altmodes (ucsi->ppm, orig, updated); + +/* now register altmodes */ +for (i =0 ; i < max_altmodes; i++) { +memset (&desc,0 ,sizeof (desc)); +if (multi_dp && recipient ==UCSI_RECIPIENT_CON ) { + desc.svid = updated[i].svid; + desc.vdo = updated[i].mid; + }else { + desc.svid = orig[i].svid; + desc.vdo = orig[i].mid; } + desc.roles =TYPEC_PORT_DRD ; + +if (!desc.svid) +return 0 ; + + ret =ucsi_register_altmode (con, &desc, recipient); +if (ret) +return ret; }return 0 ;
drivers/usb/typec/ucsi/ucsi.h +8 -0
@@ -231,6 +231,11 @@ struct ucsi_control {#define UCSI_ERROR_DEAD_BATTERY BIT(5)#define UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL BIT(6) +#define UCSI_SET_NEW_CAM_ENTER (x) (((x) >> 23) & 0x1) +#define UCSI_SET_NEW_CAM_GET_AM (x) (((x) >> 24) & 0xff) +#define UCSI_SET_NEW_CAM_AM_MASK (0xff << 24) +#define UCSI_SET_NEW_CAM_SET_AM (x) (((x) & 0xff) << 24) +#define UCSI_CMD_CONNECTOR_MASK (0x7)/* Data structure filled by PPM in response to GET_CAPABILITY command. */ struct ucsi_capability {u32 attributes;
@@ -363,11 +368,14 @@ struct ucsi_data {* @data: memory location to the UCSI data structures * @cmd: UCSI command execution routine * @sync: Refresh UCSI mailbox (the data structures) +* @update_altmodes: Squashes duplicate DP altmodes */ struct ucsi_ppm {struct ucsi_data *data;int (*cmd)(struct ucsi_ppm *,struct ucsi_control *);int (*sync)(struct ucsi_ppm *); +bool (*update_altmodes)(struct ucsi_ppm *ppm,struct ucsi_altmode *orig, +struct ucsi_altmode *updated); };struct ucsi *ucsi_register_ppm (struct device *dev,struct ucsi_ppm *ppm);
drivers/usb/typec/ucsi/ucsi_ccg.c +180 -1
225 lines not on this page · raw message
Signature · 1 line
2.17.1
Heikki Krogerus
maintainerHi Ajay,
Quoted 6 lines · Ajay Gupta
From: Ajay Gupta <[email protected]>
CCGx controller used on NVIDIA GPU card has two separate display altmode for two DP pin assignments. UCSI specification doesn't prohibits using separate display altmode.
Quoted 7 lines
Current UCSI Type-C framework expects only one display altmode for all DP pin assignment. This patch squashes two separate display altmode into single altmode to support controllers with separate display altmode. We first read all the alternate modes of connector and then run through it to know if there are separate display altmodes. If so, it prepares a new port altmode set after squashing two or more separate altmodes into one.
I'm going to go over this one more time during the merge window. I have an idea how to improve this. Please bear with me. I have to make sure all options were considered.
I'm really sorry to block this for so long, but I'm just really really afraid of mixing special case solutions like this into otherwise generic code. They always make it difficult to modify the code later, and unfortunately we have them all over the kernel. Look at the serial driver 8250_core/port/*.cas an example. Ideally we should be able to "modularize" the whole alternate mode handling in the ucsi driver since there are now two different ways the PPMs can handle them instead of simply mixing the support for the new method straight into the existing structures.
thanks,
Signature · 1 line
heikki
Hi Heikki,
Quoted 25 lines
-----Original Message-----
From: Heikki Krogerus <[email protected]>
Sent: Friday, September 13, 2019 8:20 AM
To: Ajay Gupta <[email protected]>
Cc: [email protected]; Ajay Gupta <[email protected]>
Subject: Re: [PATCH v5] usb: typec: ucsi: add support for separate DP altmode devicesHi Ajay,
Quoted 6 lines · Ajay Gupta
From: Ajay Gupta <[email protected]>
CCGx controller used on NVIDIA GPU card has two separate display altmode for two DP pin assignments. UCSI specification doesn't prohibits using separate display altmode.
Quoted 7 lines
Current UCSI Type-C framework expects only one display altmode for all DP pin assignment. This patch squashes two separate display altmode into single altmode to support controllers with separate display altmode. We first read all the alternate modes of connector and then run through it to know if there are separate display altmodes. If so, it prepares a new port altmode set after squashing two or more separate altmodes into one.
Quoted 6 lines
I'm going to go over this one more time during the merge window. I have an idea how to improve this. Please bear with me. I have to make sure all options were considered.
I'm really sorry to block this for so long, but I'm just really really afraid of mixing special case solutions like this into otherwise generic code.
I can update the patch to make sure generic code flow remains as-is and special case is executed only for UCSI controllers which needs squashing of duplicate DP altmodes.
We can use the new function pointer update_altmode() added in "struct ucsi_ppm" in v5 of my patch for this purpose.
if (recipient == UCSI_RECIPIENT_CON && ucsi->ppm && ucsi->ppm->update_altmodes)
Get all the altmodes first, pass it to update_altmodes() for squashing if needed, and then register each altmodes.
else
Use the current method where we get and register one altmode at a time.
Please let me know if this looks fine to you.
Thanks
Quoted 13 lines
nvpublic
They always
make it difficult to modify the code later, and unfortunately we have them all over the kernel. Look at the serial driver 8250_core/port/*.cas an example. Ideally we should be able to "modularize" the whole alternate mode handling in the ucsi driver since there are now two different ways the PPMs can handle them instead of simply mixing the support for the new method straight into the existing structures.thanks,
--
heikki
Heikki Krogerus
maintainerQuoted 40 lines · Ajay Gupta
Hi Heikki,
Quoted 25 lines
-----Original Message-----
From: Heikki Krogerus <[email protected]>
Sent: Friday, September 13, 2019 8:20 AM
To: Ajay Gupta <[email protected]>
Cc: [email protected]; Ajay Gupta <[email protected]>
Subject: Re: [PATCH v5] usb: typec: ucsi: add support for separate DP altmode devicesHi Ajay,
Quoted 6 lines · Ajay Gupta
From: Ajay Gupta <[email protected]>
CCGx controller used on NVIDIA GPU card has two separate display altmode for two DP pin assignments. UCSI specification doesn't prohibits using separate display altmode.
Quoted 7 lines
Current UCSI Type-C framework expects only one display altmode for all DP pin assignment. This patch squashes two separate display altmode into single altmode to support controllers with separate display altmode. We first read all the alternate modes of connector and then run through it to know if there are separate display altmodes. If so, it prepares a new port altmode set after squashing two or more separate altmodes into one.
Quoted 6 lines
I'm going to go over this one more time during the merge window. I have an idea how to improve this. Please bear with me. I have to make sure all options were considered.
I'm really sorry to block this for so long, but I'm just really really afraid of mixing special case solutions like this into otherwise generic code.
I can update the patch to make sure generic code flow remains as-is and special case is executed only for UCSI controllers which needs squashing of duplicate DP altmodes.
We can use the new function pointerupdate_altmode()added in "structucsi_ppm" in v5 of my patch for this purpose.
Quoted 7 lines
if (recipient ==
UCSI_RECIPIENT_CON&&ucsi->ppm&&ucsi->ppm->update_altmodes)
Get all the altmodes first, pass it toupdate_altmodes()for squashing if needed, and then register each altmodes.
else
Use the current method where we get and register one altmode at a time.Please let me know if this looks fine to you.
No. Please wait. I'm refactoring ucsi.c. I'm going to redo the I/O completely, and also do some long waited cleanups. Those changes should make handling this case also much easier.
thanks,
Signature · 1 line
heikki