Skip to content

Commit cc87d3d

Browse files
CoelacanthusHexgregkh
authored andcommitted
can: gs_usb: increase max interface to U8_MAX
commit 2a27f6a upstream. This issue was found by Runcheng Lu when develop HSCanT USB to CAN FD converter[1]. The original developers may have only 3 interfaces device to test so they write 3 here and wait for future change. During the HSCanT development, we actually used 4 interfaces, so the limitation of 3 is not enough now. But just increase one is not future-proofed. Since the channel index type in gs_host_frame is u8, just make canch[] become a flexible array with a u8 index, so it naturally constraint by U8_MAX and avoid statically allocate 256 pointer for every gs_usb device. [1]: https://github.com/cherry-embedded/HSCanT-hardware Fixes: d08e973 ("can: gs_usb: Added support for the GS_USB CAN devices") Reported-by: Runcheng Lu <[email protected]> Cc: [email protected] Reviewed-by: Vincent Mailhol <[email protected]> Signed-off-by: Celeste Liu <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Marc Kleine-Budde <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 52eb720 commit cc87d3d

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

drivers/net/can/usb/gs_usb.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,6 @@ struct gs_host_frame {
286286
#define GS_MAX_RX_URBS 30
287287
#define GS_NAPI_WEIGHT 32
288288

289-
/* Maximum number of interfaces the driver supports per device.
290-
* Current hardware only supports 3 interfaces. The future may vary.
291-
*/
292-
#define GS_MAX_INTF 3
293-
294289
struct gs_tx_context {
295290
struct gs_can *dev;
296291
unsigned int echo_id;
@@ -321,7 +316,6 @@ struct gs_can {
321316

322317
/* usb interface struct */
323318
struct gs_usb {
324-
struct gs_can *canch[GS_MAX_INTF];
325319
struct usb_anchor rx_submitted;
326320
struct usb_device *udev;
327321

@@ -333,9 +327,11 @@ struct gs_usb {
333327

334328
unsigned int hf_size_rx;
335329
u8 active_channels;
330+
u8 channel_cnt;
336331

337332
unsigned int pipe_in;
338333
unsigned int pipe_out;
334+
struct gs_can *canch[] __counted_by(channel_cnt);
339335
};
340336

341337
/* 'allocate' a tx context.
@@ -596,7 +592,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
596592
}
597593

598594
/* device reports out of range channel id */
599-
if (hf->channel >= GS_MAX_INTF)
595+
if (hf->channel >= parent->channel_cnt)
600596
goto device_detach;
601597

602598
dev = parent->canch[hf->channel];
@@ -696,7 +692,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
696692
/* USB failure take down all interfaces */
697693
if (rc == -ENODEV) {
698694
device_detach:
699-
for (rc = 0; rc < GS_MAX_INTF; rc++) {
695+
for (rc = 0; rc < parent->channel_cnt; rc++) {
700696
if (parent->canch[rc])
701697
netif_device_detach(parent->canch[rc]->netdev);
702698
}
@@ -1458,17 +1454,19 @@ static int gs_usb_probe(struct usb_interface *intf,
14581454
icount = dconf.icount + 1;
14591455
dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
14601456

1461-
if (icount > GS_MAX_INTF) {
1457+
if (icount > type_max(parent->channel_cnt)) {
14621458
dev_err(&intf->dev,
14631459
"Driver cannot handle more that %u CAN interfaces\n",
1464-
GS_MAX_INTF);
1460+
type_max(parent->channel_cnt));
14651461
return -EINVAL;
14661462
}
14671463

1468-
parent = kzalloc(sizeof(*parent), GFP_KERNEL);
1464+
parent = kzalloc(struct_size(parent, canch, icount), GFP_KERNEL);
14691465
if (!parent)
14701466
return -ENOMEM;
14711467

1468+
parent->channel_cnt = icount;
1469+
14721470
init_usb_anchor(&parent->rx_submitted);
14731471

14741472
usb_set_intfdata(intf, parent);
@@ -1529,7 +1527,7 @@ static void gs_usb_disconnect(struct usb_interface *intf)
15291527
return;
15301528
}
15311529

1532-
for (i = 0; i < GS_MAX_INTF; i++)
1530+
for (i = 0; i < parent->channel_cnt; i++)
15331531
if (parent->canch[i])
15341532
gs_destroy_candev(parent->canch[i]);
15351533

0 commit comments

Comments
 (0)