Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: pxrc - flatten probe code

Instead of splitting probe code into separate USB and input setup, flatten it.
This allows for easier inspection of order of set up steps, since the probe code
is reasonably small.

Move input-related initialization (phys) from USB block to input block.

Reviewed-by: Marcus Folkesson <marcus.folkesson@gmail.com>
Tested-by: Marcus Folkesson <marcus.folkesson@gmail.com>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+35 -49
+35 -49
drivers/input/joystick/pxrc.c
··· 3 3 * Driver for Phoenix RC Flight Controller Adapter 4 4 * 5 5 * Copyright (C) 2018 Marcus Folkesson <marcus.folkesson@gmail.com> 6 - * 7 6 */ 8 7 9 8 #include <linux/kernel.h> ··· 15 16 #include <linux/mutex.h> 16 17 #include <linux/input.h> 17 18 18 - #define PXRC_VENDOR_ID (0x1781) 19 - #define PXRC_PRODUCT_ID (0x0898) 19 + #define PXRC_VENDOR_ID 0x1781 20 + #define PXRC_PRODUCT_ID 0x0898 20 21 21 22 struct pxrc { 22 23 struct input_dev *input; ··· 117 118 usb_free_urb(pxrc->urb); 118 119 } 119 120 120 - static int pxrc_usb_init(struct pxrc *pxrc) 121 + static int pxrc_probe(struct usb_interface *intf, 122 + const struct usb_device_id *id) 121 123 { 122 - struct usb_device *udev = interface_to_usbdev(pxrc->intf); 124 + struct usb_device *udev = interface_to_usbdev(intf); 125 + struct pxrc *pxrc; 123 126 struct usb_endpoint_descriptor *epirq; 124 127 size_t xfer_size; 125 128 void *xfer_buf; 126 - unsigned int pipe; 127 129 int error; 128 130 129 - /* Set up the endpoint information */ 130 - /* This device only has an interrupt endpoint */ 131 - error = usb_find_common_endpoints(pxrc->intf->cur_altsetting, 131 + /* 132 + * Locate the endpoint information. This device only has an 133 + * interrupt endpoint. 134 + */ 135 + error = usb_find_common_endpoints(intf->cur_altsetting, 132 136 NULL, NULL, &epirq, NULL); 133 137 if (error) { 134 - dev_err(&pxrc->intf->dev, "Could not find endpoint\n"); 138 + dev_err(&intf->dev, "Could not find endpoint\n"); 135 139 return error; 136 140 } 137 141 138 - xfer_size = usb_endpoint_maxp(epirq); 139 - xfer_buf = devm_kmalloc(&pxrc->intf->dev, xfer_size, GFP_KERNEL); 140 - if (!xfer_buf) 142 + pxrc = devm_kzalloc(&intf->dev, sizeof(*pxrc), GFP_KERNEL); 143 + if (!pxrc) 141 144 return -ENOMEM; 142 145 146 + mutex_init(&pxrc->pm_mutex); 147 + pxrc->intf = intf; 148 + 143 149 usb_set_intfdata(pxrc->intf, pxrc); 144 - usb_make_path(udev, pxrc->phys, sizeof(pxrc->phys)); 145 - strlcat(pxrc->phys, "/input0", sizeof(pxrc->phys)); 150 + 151 + xfer_size = usb_endpoint_maxp(epirq); 152 + xfer_buf = devm_kmalloc(&intf->dev, xfer_size, GFP_KERNEL); 153 + if (!xfer_buf) 154 + return -ENOMEM; 146 155 147 156 pxrc->urb = usb_alloc_urb(0, GFP_KERNEL); 148 157 if (!pxrc->urb) 149 158 return -ENOMEM; 150 159 151 - error = devm_add_action_or_reset(&pxrc->intf->dev, pxrc_free_urb, pxrc); 160 + error = devm_add_action_or_reset(&intf->dev, pxrc_free_urb, pxrc); 152 161 if (error) 153 162 return error; 154 163 155 - pipe = usb_rcvintpipe(udev, epirq->bEndpointAddress), 156 - usb_fill_int_urb(pxrc->urb, udev, pipe, xfer_buf, xfer_size, 157 - pxrc_usb_irq, pxrc, 1); 164 + usb_fill_int_urb(pxrc->urb, udev, 165 + usb_rcvintpipe(udev, epirq->bEndpointAddress), 166 + xfer_buf, xfer_size, pxrc_usb_irq, pxrc, 1); 158 167 159 - return 0; 160 - } 161 - 162 - static int pxrc_input_init(struct pxrc *pxrc) 163 - { 164 - pxrc->input = devm_input_allocate_device(&pxrc->intf->dev); 165 - if (pxrc->input == NULL) { 166 - dev_err(&pxrc->intf->dev, "couldn't allocate input device\n"); 168 + pxrc->input = devm_input_allocate_device(&intf->dev); 169 + if (!pxrc->input) { 170 + dev_err(&intf->dev, "couldn't allocate input device\n"); 167 171 return -ENOMEM; 168 172 } 169 173 170 174 pxrc->input->name = "PXRC Flight Controller Adapter"; 175 + 176 + usb_make_path(udev, pxrc->phys, sizeof(pxrc->phys)); 177 + strlcat(pxrc->phys, "/input0", sizeof(pxrc->phys)); 171 178 pxrc->input->phys = pxrc->phys; 172 - usb_to_input_id(interface_to_usbdev(pxrc->intf), &pxrc->input->id); 179 + 180 + usb_to_input_id(udev, &pxrc->input->id); 173 181 174 182 pxrc->input->open = pxrc_open; 175 183 pxrc->input->close = pxrc_close; ··· 192 186 193 187 input_set_drvdata(pxrc->input, pxrc); 194 188 195 - return input_register_device(pxrc->input); 196 - } 197 - 198 - static int pxrc_probe(struct usb_interface *intf, 199 - const struct usb_device_id *id) 200 - { 201 - struct pxrc *pxrc; 202 - int error; 203 - 204 - pxrc = devm_kzalloc(&intf->dev, sizeof(*pxrc), GFP_KERNEL); 205 - if (!pxrc) 206 - return -ENOMEM; 207 - 208 - mutex_init(&pxrc->pm_mutex); 209 - pxrc->intf = intf; 210 - 211 - error = pxrc_usb_init(pxrc); 212 - if (error) 213 - return error; 214 - 215 - error = pxrc_input_init(pxrc); 189 + error = input_register_device(pxrc->input); 216 190 if (error) 217 191 return error; 218 192