Skip to content

Commit 2a27f6a

Browse files
CoelacanthusHexmarckleinebudde
authored andcommitted
can: gs_usb: increase max interface to U8_MAX
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]>
1 parent 2c95a75 commit 2a27f6a

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
@@ -289,11 +289,6 @@ struct gs_host_frame {
289289
#define GS_MAX_RX_URBS 30
290290
#define GS_NAPI_WEIGHT 32
291291

292-
/* Maximum number of interfaces the driver supports per device.
293-
* Current hardware only supports 3 interfaces. The future may vary.
294-
*/
295-
#define GS_MAX_INTF 3
296-
297292
struct gs_tx_context {
298293
struct gs_can *dev;
299294
unsigned int echo_id;
@@ -324,7 +319,6 @@ struct gs_can {
324319

325320
/* usb interface struct */
326321
struct gs_usb {
327-
struct gs_can *canch[GS_MAX_INTF];
328322
struct usb_anchor rx_submitted;
329323
struct usb_device *udev;
330324

@@ -336,9 +330,11 @@ struct gs_usb {
336330

337331
unsigned int hf_size_rx;
338332
u8 active_channels;
333+
u8 channel_cnt;
339334

340335
unsigned int pipe_in;
341336
unsigned int pipe_out;
337+
struct gs_can *canch[] __counted_by(channel_cnt);
342338
};
343339

344340
/* 'allocate' a tx context.
@@ -599,7 +595,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
599595
}
600596

601597
/* device reports out of range channel id */
602-
if (hf->channel >= GS_MAX_INTF)
598+
if (hf->channel >= parent->channel_cnt)
603599
goto device_detach;
604600

605601
dev = parent->canch[hf->channel];
@@ -699,7 +695,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
699695
/* USB failure take down all interfaces */
700696
if (rc == -ENODEV) {
701697
device_detach:
702-
for (rc = 0; rc < GS_MAX_INTF; rc++) {
698+
for (rc = 0; rc < parent->channel_cnt; rc++) {
703699
if (parent->canch[rc])
704700
netif_device_detach(parent->canch[rc]->netdev);
705701
}
@@ -1460,17 +1456,19 @@ static int gs_usb_probe(struct usb_interface *intf,
14601456
icount = dconf.icount + 1;
14611457
dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
14621458

1463-
if (icount > GS_MAX_INTF) {
1459+
if (icount > type_max(parent->channel_cnt)) {
14641460
dev_err(&intf->dev,
14651461
"Driver cannot handle more that %u CAN interfaces\n",
1466-
GS_MAX_INTF);
1462+
type_max(parent->channel_cnt));
14671463
return -EINVAL;
14681464
}
14691465

1470-
parent = kzalloc(sizeof(*parent), GFP_KERNEL);
1466+
parent = kzalloc(struct_size(parent, canch, icount), GFP_KERNEL);
14711467
if (!parent)
14721468
return -ENOMEM;
14731469

1470+
parent->channel_cnt = icount;
1471+
14741472
init_usb_anchor(&parent->rx_submitted);
14751473

14761474
usb_set_intfdata(intf, parent);
@@ -1531,7 +1529,7 @@ static void gs_usb_disconnect(struct usb_interface *intf)
15311529
return;
15321530
}
15331531

1534-
for (i = 0; i < GS_MAX_INTF; i++)
1532+
for (i = 0; i < parent->channel_cnt; i++)
15351533
if (parent->canch[i])
15361534
gs_destroy_candev(parent->canch[i]);
15371535

0 commit comments

Comments
 (0)