ipack: ipoctal: fix tty-registration error handling

Registration of the ipoctal tty devices is unlikely to fail, but if it
ever does, make sure not to deregister a never registered tty device
(and dereference a NULL pointer) when the driver is later unbound.

Fixes: 2afb41d9d30d ("Staging: ipack/devices/ipoctal: Check tty_register_device return value.")
Cc: stable@vger.kernel.org # 3.7
Acked-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
Link: https://lore.kernel.org/r/20210917114622.5412-4-johan@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by Johan Hovold and committed by Greg Kroah-Hartman cd20d592 65c001df

+7
+7
drivers/ipack/devices/ipoctal.c
··· 33 unsigned int pointer_read; 34 unsigned int pointer_write; 35 struct tty_port tty_port; 36 union scc2698_channel __iomem *regs; 37 union scc2698_block __iomem *block_regs; 38 unsigned int board_id; ··· 397 i, NULL, channel, NULL); 398 if (IS_ERR(tty_dev)) { 399 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n"); 400 tty_port_destroy(&channel->tty_port); 401 continue; 402 } 403 } 404 405 /* ··· 701 702 for (i = 0; i < NR_CHANNELS; i++) { 703 struct ipoctal_channel *channel = &ipoctal->channel[i]; 704 tty_unregister_device(ipoctal->tty_drv, i); 705 tty_port_free_xmit_buf(&channel->tty_port); 706 tty_port_destroy(&channel->tty_port);
··· 33 unsigned int pointer_read; 34 unsigned int pointer_write; 35 struct tty_port tty_port; 36 + bool tty_registered; 37 union scc2698_channel __iomem *regs; 38 union scc2698_block __iomem *block_regs; 39 unsigned int board_id; ··· 396 i, NULL, channel, NULL); 397 if (IS_ERR(tty_dev)) { 398 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n"); 399 + tty_port_free_xmit_buf(&channel->tty_port); 400 tty_port_destroy(&channel->tty_port); 401 continue; 402 } 403 + channel->tty_registered = true; 404 } 405 406 /* ··· 698 699 for (i = 0; i < NR_CHANNELS; i++) { 700 struct ipoctal_channel *channel = &ipoctal->channel[i]; 701 + 702 + if (!channel->tty_registered) 703 + continue; 704 + 705 tty_unregister_device(ipoctal->tty_drv, i); 706 tty_port_free_xmit_buf(&channel->tty_port); 707 tty_port_destroy(&channel->tty_port);