at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * FTDI MPSSE GPIO support |
| 4 | * |
| 5 | * Based on code by Anatolij Gustschin |
| 6 | * |
| 7 | * Copyright (C) 2024 Mary Strodl <mstrodl@csh.rit.edu> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/cleanup.h> |
| 11 | #include <linux/gpio/driver.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/rcupdate.h> |
| 14 | #include <linux/spinlock.h> |
| 15 | #include <linux/usb.h> |
| 16 | |
| 17 | struct mpsse_priv { |
| 18 | struct gpio_chip gpio; |
| 19 | struct usb_device *udev; /* USB device encompassing all MPSSEs */ |
| 20 | struct usb_interface *intf; /* USB interface for this MPSSE */ |
| 21 | u8 intf_id; /* USB interface number for this MPSSE */ |
| 22 | struct list_head workers; /* polling work threads */ |
| 23 | struct mutex irq_mutex; /* lock over irq_data */ |
| 24 | struct mutex irq_race; /* race for polling worker teardown */ |
| 25 | raw_spinlock_t irq_spin; /* protects worker list */ |
| 26 | atomic_t irq_type[16]; /* pin -> edge detection type */ |
| 27 | atomic_t irq_enabled; |
| 28 | atomic_t dying; /* no new workers after disconnect */ |
| 29 | int id; |
| 30 | |
| 31 | u8 gpio_outputs[2]; /* Output states for GPIOs [L, H] */ |
| 32 | u8 gpio_dir[2]; /* Directions for GPIOs [L, H] */ |
| 33 | |
| 34 | unsigned long dir_in; /* Bitmask of valid input pins */ |
| 35 | unsigned long dir_out; /* Bitmask of valid output pins */ |
| 36 | |
| 37 | u8 *bulk_in_buf; /* Extra recv buffer to grab status bytes */ |
| 38 | |
| 39 | struct usb_endpoint_descriptor *bulk_in; |
| 40 | struct usb_endpoint_descriptor *bulk_out; |
| 41 | |
| 42 | struct mutex io_mutex; /* sync I/O with disconnect */ |
| 43 | }; |
| 44 | |
| 45 | struct mpsse_worker { |
| 46 | struct mpsse_priv *priv; |
| 47 | struct work_struct work; |
| 48 | atomic_t cancelled; |
| 49 | struct list_head list; /* linked list */ |
| 50 | struct list_head destroy; /* teardown linked list */ |
| 51 | struct rcu_head rcu; /* deferred free for dying path */ |
| 52 | }; |
| 53 | |
| 54 | struct bulk_desc { |
| 55 | bool tx; /* direction of bulk transfer */ |
| 56 | u8 *data; /* input (tx) or output (rx) */ |
| 57 | int len; /* Length of `data` if tx, or length of */ |
| 58 | /* Data to read if rx */ |
| 59 | int len_actual; /* Length successfully transferred */ |
| 60 | int timeout; |
| 61 | }; |
| 62 | |
| 63 | #define MPSSE_NGPIO 16 |
| 64 | |
| 65 | struct mpsse_quirk { |
| 66 | const char *names[MPSSE_NGPIO]; /* Pin names, if applicable */ |
| 67 | unsigned long dir_in; /* Bitmask of valid input pins */ |
| 68 | unsigned long dir_out; /* Bitmask of valid output pins */ |
| 69 | }; |
| 70 | |
| 71 | static struct mpsse_quirk bryx_brik_quirk = { |
| 72 | .names = { |
| 73 | [3] = "Push to Talk", |
| 74 | [5] = "Channel Activity", |
| 75 | }, |
| 76 | .dir_out = BIT(3), /* Push to Talk */ |
| 77 | .dir_in = BIT(5), /* Channel Activity */ |
| 78 | }; |
| 79 | |
| 80 | static const struct usb_device_id gpio_mpsse_table[] = { |
| 81 | { USB_DEVICE(0x0c52, 0xa064) }, /* SeaLevel Systems, Inc. */ |
| 82 | { USB_DEVICE(0x0403, 0x6988), /* FTDI, assigned to Bryx */ |
| 83 | .driver_info = (kernel_ulong_t)&bryx_brik_quirk}, |
| 84 | { } /* Terminating entry */ |
| 85 | }; |
| 86 | |
| 87 | MODULE_DEVICE_TABLE(usb, gpio_mpsse_table); |
| 88 | |
| 89 | static DEFINE_IDA(gpio_mpsse_ida); |
| 90 | |
| 91 | /* MPSSE commands */ |
| 92 | #define SET_BITS_CMD 0x80 |
| 93 | #define GET_BITS_CMD 0x81 |
| 94 | |
| 95 | #define SET_BITMODE_REQUEST 0x0B |
| 96 | #define MODE_MPSSE (2 << 8) |
| 97 | #define MODE_RESET 0 |
| 98 | |
| 99 | /* Arbitrarily decided. This could probably be much less */ |
| 100 | #define MPSSE_WRITE_TIMEOUT 5000 |
| 101 | #define MPSSE_READ_TIMEOUT 5000 |
| 102 | |
| 103 | /* 1 millisecond, also pretty arbitrary */ |
| 104 | #define MPSSE_POLL_INTERVAL 1000 |
| 105 | |
| 106 | static int mpsse_bulk_xfer(struct usb_interface *intf, struct bulk_desc *desc) |
| 107 | { |
| 108 | struct mpsse_priv *priv = usb_get_intfdata(intf); |
| 109 | struct usb_device *udev = priv->udev; |
| 110 | unsigned int pipe; |
| 111 | int ret; |
| 112 | |
| 113 | if (desc->tx) |
| 114 | pipe = usb_sndbulkpipe(udev, priv->bulk_out->bEndpointAddress); |
| 115 | else |
| 116 | pipe = usb_rcvbulkpipe(udev, priv->bulk_in->bEndpointAddress); |
| 117 | |
| 118 | ret = usb_bulk_msg(udev, pipe, desc->data, desc->len, |
| 119 | &desc->len_actual, desc->timeout); |
| 120 | if (ret) |
| 121 | dev_dbg(&udev->dev, "mpsse: bulk transfer failed: %d\n", ret); |
| 122 | |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | static int mpsse_write(struct usb_interface *intf, |
| 127 | u8 *buf, size_t len) |
| 128 | { |
| 129 | int ret; |
| 130 | struct bulk_desc desc; |
| 131 | |
| 132 | desc.len_actual = 0; |
| 133 | desc.tx = true; |
| 134 | desc.data = buf; |
| 135 | desc.len = len; |
| 136 | desc.timeout = MPSSE_WRITE_TIMEOUT; |
| 137 | |
| 138 | ret = mpsse_bulk_xfer(intf, &desc); |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | static int mpsse_read(struct usb_interface *intf, u8 *buf, size_t len) |
| 144 | { |
| 145 | int ret; |
| 146 | struct bulk_desc desc; |
| 147 | struct mpsse_priv *priv = usb_get_intfdata(intf); |
| 148 | |
| 149 | desc.len_actual = 0; |
| 150 | desc.tx = false; |
| 151 | desc.data = priv->bulk_in_buf; |
| 152 | /* Device sends 2 additional status bytes, read len + 2 */ |
| 153 | desc.len = min_t(size_t, len + 2, usb_endpoint_maxp(priv->bulk_in)); |
| 154 | desc.timeout = MPSSE_READ_TIMEOUT; |
| 155 | |
| 156 | ret = mpsse_bulk_xfer(intf, &desc); |
| 157 | if (ret) |
| 158 | return ret; |
| 159 | |
| 160 | /* Did we get enough data? */ |
| 161 | if (desc.len_actual < desc.len) |
| 162 | return -EIO; |
| 163 | |
| 164 | memcpy(buf, desc.data + 2, desc.len_actual - 2); |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | static int gpio_mpsse_set_bank(struct mpsse_priv *priv, u8 bank) |
| 170 | { |
| 171 | int ret; |
| 172 | u8 tx_buf[3] = { |
| 173 | SET_BITS_CMD | (bank << 1), |
| 174 | priv->gpio_outputs[bank], |
| 175 | priv->gpio_dir[bank], |
| 176 | }; |
| 177 | |
| 178 | ret = mpsse_write(priv->intf, tx_buf, 3); |
| 179 | |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static int gpio_mpsse_get_bank(struct mpsse_priv *priv, u8 bank) |
| 184 | { |
| 185 | int ret; |
| 186 | u8 buf = GET_BITS_CMD | (bank << 1); |
| 187 | |
| 188 | ret = mpsse_write(priv->intf, &buf, 1); |
| 189 | if (ret) |
| 190 | return ret; |
| 191 | |
| 192 | ret = mpsse_read(priv->intf, &buf, 1); |
| 193 | if (ret) |
| 194 | return ret; |
| 195 | |
| 196 | return buf; |
| 197 | } |
| 198 | |
| 199 | static int mpsse_ensure_supported(struct gpio_chip *chip, |
| 200 | unsigned long mask, int direction) |
| 201 | { |
| 202 | unsigned long supported, unsupported; |
| 203 | char *type = "input"; |
| 204 | struct mpsse_priv *priv = gpiochip_get_data(chip); |
| 205 | |
| 206 | supported = priv->dir_in; |
| 207 | if (direction == GPIO_LINE_DIRECTION_OUT) { |
| 208 | supported = priv->dir_out; |
| 209 | type = "output"; |
| 210 | } |
| 211 | |
| 212 | /* An invalid bit was in the provided mask */ |
| 213 | unsupported = mask & ~supported; |
| 214 | if (unsupported) { |
| 215 | dev_err(&priv->udev->dev, |
| 216 | "mpsse: GPIO %lu doesn't support %s\n", |
| 217 | find_first_bit(&unsupported, sizeof(unsupported) * 8), |
| 218 | type); |
| 219 | return -EOPNOTSUPP; |
| 220 | } |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int gpio_mpsse_set_multiple(struct gpio_chip *chip, unsigned long *mask, |
| 226 | unsigned long *bits) |
| 227 | { |
| 228 | unsigned long i, bank, bank_mask, bank_bits; |
| 229 | int ret; |
| 230 | struct mpsse_priv *priv = gpiochip_get_data(chip); |
| 231 | |
| 232 | ret = mpsse_ensure_supported(chip, *mask, GPIO_LINE_DIRECTION_OUT); |
| 233 | if (ret) |
| 234 | return ret; |
| 235 | |
| 236 | guard(mutex)(&priv->io_mutex); |
| 237 | for_each_set_clump8(i, bank_mask, mask, chip->ngpio) { |
| 238 | bank = i / 8; |
| 239 | |
| 240 | if (bank_mask) { |
| 241 | bank_bits = bitmap_get_value8(bits, i); |
| 242 | /* Zero out pins we want to change */ |
| 243 | priv->gpio_outputs[bank] &= ~bank_mask; |
| 244 | /* Set pins we care about */ |
| 245 | priv->gpio_outputs[bank] |= bank_bits & bank_mask; |
| 246 | |
| 247 | ret = gpio_mpsse_set_bank(priv, bank); |
| 248 | if (ret) |
| 249 | return ret; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int gpio_mpsse_get_multiple(struct gpio_chip *chip, unsigned long *mask, |
| 257 | unsigned long *bits) |
| 258 | { |
| 259 | unsigned long i, bank, bank_mask; |
| 260 | int ret; |
| 261 | struct mpsse_priv *priv = gpiochip_get_data(chip); |
| 262 | |
| 263 | ret = mpsse_ensure_supported(chip, *mask, GPIO_LINE_DIRECTION_IN); |
| 264 | if (ret) |
| 265 | return ret; |
| 266 | |
| 267 | guard(mutex)(&priv->io_mutex); |
| 268 | for_each_set_clump8(i, bank_mask, mask, chip->ngpio) { |
| 269 | bank = i / 8; |
| 270 | |
| 271 | if (bank_mask) { |
| 272 | ret = gpio_mpsse_get_bank(priv, bank); |
| 273 | if (ret < 0) |
| 274 | return ret; |
| 275 | |
| 276 | bitmap_set_value8(bits, ret & bank_mask, i); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int gpio_mpsse_gpio_get(struct gpio_chip *chip, unsigned int offset) |
| 284 | { |
| 285 | int err; |
| 286 | unsigned long mask = 0, bits = 0; |
| 287 | |
| 288 | __set_bit(offset, &mask); |
| 289 | err = gpio_mpsse_get_multiple(chip, &mask, &bits); |
| 290 | if (err) |
| 291 | return err; |
| 292 | |
| 293 | /* == is not guaranteed to give 1 if true */ |
| 294 | if (bits) |
| 295 | return 1; |
| 296 | else |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static int gpio_mpsse_gpio_set(struct gpio_chip *chip, unsigned int offset, |
| 301 | int value) |
| 302 | { |
| 303 | unsigned long mask = 0, bits = 0; |
| 304 | |
| 305 | __set_bit(offset, &mask); |
| 306 | if (value) |
| 307 | __set_bit(offset, &bits); |
| 308 | |
| 309 | return gpio_mpsse_set_multiple(chip, &mask, &bits); |
| 310 | } |
| 311 | |
| 312 | static int gpio_mpsse_direction_output(struct gpio_chip *chip, |
| 313 | unsigned int offset, int value) |
| 314 | { |
| 315 | int ret; |
| 316 | struct mpsse_priv *priv = gpiochip_get_data(chip); |
| 317 | int bank = (offset & 8) >> 3; |
| 318 | int bank_offset = offset & 7; |
| 319 | |
| 320 | ret = mpsse_ensure_supported(chip, BIT(offset), GPIO_LINE_DIRECTION_OUT); |
| 321 | if (ret) |
| 322 | return ret; |
| 323 | |
| 324 | scoped_guard(mutex, &priv->io_mutex) |
| 325 | priv->gpio_dir[bank] |= BIT(bank_offset); |
| 326 | |
| 327 | return gpio_mpsse_gpio_set(chip, offset, value); |
| 328 | } |
| 329 | |
| 330 | static int gpio_mpsse_direction_input(struct gpio_chip *chip, |
| 331 | unsigned int offset) |
| 332 | { |
| 333 | int ret; |
| 334 | struct mpsse_priv *priv = gpiochip_get_data(chip); |
| 335 | int bank = (offset & 8) >> 3; |
| 336 | int bank_offset = offset & 7; |
| 337 | |
| 338 | ret = mpsse_ensure_supported(chip, BIT(offset), GPIO_LINE_DIRECTION_IN); |
| 339 | if (ret) |
| 340 | return ret; |
| 341 | |
| 342 | guard(mutex)(&priv->io_mutex); |
| 343 | priv->gpio_dir[bank] &= ~BIT(bank_offset); |
| 344 | |
| 345 | return gpio_mpsse_set_bank(priv, bank); |
| 346 | } |
| 347 | |
| 348 | static int gpio_mpsse_get_direction(struct gpio_chip *chip, |
| 349 | unsigned int offset) |
| 350 | { |
| 351 | int ret; |
| 352 | int bank = (offset & 8) >> 3; |
| 353 | int bank_offset = offset & 7; |
| 354 | struct mpsse_priv *priv = gpiochip_get_data(chip); |
| 355 | |
| 356 | guard(mutex)(&priv->io_mutex); |
| 357 | /* MPSSE directions are inverted */ |
| 358 | if (priv->gpio_dir[bank] & BIT(bank_offset)) |
| 359 | ret = GPIO_LINE_DIRECTION_OUT; |
| 360 | else |
| 361 | ret = GPIO_LINE_DIRECTION_IN; |
| 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Stops all workers except `my_worker`. |
| 368 | * Safe to call only when `irq_race` is held. |
| 369 | */ |
| 370 | static void gpio_mpsse_stop_all_except(struct mpsse_priv *priv, |
| 371 | struct mpsse_worker *my_worker) |
| 372 | { |
| 373 | struct mpsse_worker *worker, *worker_tmp; |
| 374 | struct list_head destructors = LIST_HEAD_INIT(destructors); |
| 375 | |
| 376 | scoped_guard(raw_spinlock_irqsave, &priv->irq_spin) { |
| 377 | list_for_each_entry_safe(worker, worker_tmp, |
| 378 | &priv->workers, list) { |
| 379 | /* Don't stop ourselves */ |
| 380 | if (worker == my_worker) |
| 381 | continue; |
| 382 | |
| 383 | list_del(&worker->list); |
| 384 | |
| 385 | /* Give worker a chance to terminate itself */ |
| 386 | atomic_set(&worker->cancelled, 1); |
| 387 | /* Keep track of stuff to cancel */ |
| 388 | INIT_LIST_HEAD(&worker->destroy); |
| 389 | list_add(&worker->destroy, &destructors); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | list_for_each_entry_safe(worker, worker_tmp, |
| 394 | &destructors, destroy) { |
| 395 | list_del(&worker->destroy); |
| 396 | cancel_work_sync(&worker->work); |
| 397 | kfree(worker); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static void gpio_mpsse_poll(struct work_struct *my_work) |
| 402 | { |
| 403 | unsigned long pin_mask, pin_states, flags; |
| 404 | int irq_enabled, offset, err, value, fire_irq, |
| 405 | irq, old_value[16], irq_type[16]; |
| 406 | struct mpsse_worker *my_worker = container_of(my_work, struct mpsse_worker, work); |
| 407 | struct mpsse_priv *priv = my_worker->priv; |
| 408 | |
| 409 | for (offset = 0; offset < priv->gpio.ngpio; ++offset) |
| 410 | old_value[offset] = -1; |
| 411 | |
| 412 | /* |
| 413 | * We only want one worker. Workers race to acquire irq_race and tear |
| 414 | * down all other workers. This is a cond guard so that we don't deadlock |
| 415 | * trying to cancel a worker. |
| 416 | */ |
| 417 | scoped_cond_guard(mutex_try, return, &priv->irq_race) |
| 418 | gpio_mpsse_stop_all_except(priv, my_worker); |
| 419 | |
| 420 | while ((irq_enabled = atomic_read(&priv->irq_enabled)) && |
| 421 | !atomic_read(&my_worker->cancelled)) { |
| 422 | usleep_range(MPSSE_POLL_INTERVAL, MPSSE_POLL_INTERVAL + 1000); |
| 423 | /* Cleanup will trigger at the end of the loop */ |
| 424 | guard(mutex)(&priv->irq_mutex); |
| 425 | |
| 426 | pin_mask = 0; |
| 427 | pin_states = 0; |
| 428 | for (offset = 0; offset < priv->gpio.ngpio; ++offset) { |
| 429 | irq_type[offset] = atomic_read(&priv->irq_type[offset]); |
| 430 | if (irq_type[offset] != IRQ_TYPE_NONE && |
| 431 | irq_enabled & BIT(offset)) |
| 432 | pin_mask |= BIT(offset); |
| 433 | else |
| 434 | old_value[offset] = -1; |
| 435 | } |
| 436 | |
| 437 | err = gpio_mpsse_get_multiple(&priv->gpio, &pin_mask, |
| 438 | &pin_states); |
| 439 | if (err) { |
| 440 | dev_err_ratelimited(&priv->intf->dev, |
| 441 | "Error polling!\n"); |
| 442 | continue; |
| 443 | } |
| 444 | |
| 445 | /* Check each value */ |
| 446 | for (offset = 0; offset < priv->gpio.ngpio; ++offset) { |
| 447 | if (old_value[offset] == -1) |
| 448 | continue; |
| 449 | |
| 450 | fire_irq = 0; |
| 451 | value = pin_states & BIT(offset); |
| 452 | |
| 453 | switch (irq_type[offset]) { |
| 454 | case IRQ_TYPE_EDGE_RISING: |
| 455 | fire_irq = value > old_value[offset]; |
| 456 | break; |
| 457 | case IRQ_TYPE_EDGE_FALLING: |
| 458 | fire_irq = value < old_value[offset]; |
| 459 | break; |
| 460 | case IRQ_TYPE_EDGE_BOTH: |
| 461 | fire_irq = value != old_value[offset]; |
| 462 | break; |
| 463 | } |
| 464 | if (!fire_irq) |
| 465 | continue; |
| 466 | |
| 467 | irq = irq_find_mapping(priv->gpio.irq.domain, |
| 468 | offset); |
| 469 | local_irq_save(flags); |
| 470 | generic_handle_irq(irq); |
| 471 | local_irq_disable(); |
| 472 | local_irq_restore(flags); |
| 473 | } |
| 474 | |
| 475 | /* Sync back values so we can refer to them next tick */ |
| 476 | for (offset = 0; offset < priv->gpio.ngpio; ++offset) |
| 477 | if (irq_type[offset] != IRQ_TYPE_NONE && |
| 478 | irq_enabled & BIT(offset)) |
| 479 | old_value[offset] = pin_states & BIT(offset); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | static int gpio_mpsse_set_irq_type(struct irq_data *irqd, unsigned int type) |
| 484 | { |
| 485 | int offset; |
| 486 | struct mpsse_priv *priv = irq_data_get_irq_chip_data(irqd); |
| 487 | |
| 488 | offset = irqd->hwirq; |
| 489 | atomic_set(&priv->irq_type[offset], type & IRQ_TYPE_EDGE_BOTH); |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | static void gpio_mpsse_irq_disable(struct irq_data *irqd) |
| 495 | { |
| 496 | struct mpsse_worker *worker; |
| 497 | struct mpsse_priv *priv = irq_data_get_irq_chip_data(irqd); |
| 498 | |
| 499 | atomic_and(~BIT(irqd->hwirq), &priv->irq_enabled); |
| 500 | gpiochip_disable_irq(&priv->gpio, irqd->hwirq); |
| 501 | |
| 502 | /* |
| 503 | * Can't actually do teardown in IRQ context (it blocks). |
| 504 | * As a result, these workers will stick around until irq is reenabled |
| 505 | * or device gets disconnected |
| 506 | */ |
| 507 | scoped_guard(raw_spinlock_irqsave, &priv->irq_spin) |
| 508 | list_for_each_entry(worker, &priv->workers, list) |
| 509 | atomic_set(&worker->cancelled, 1); |
| 510 | } |
| 511 | |
| 512 | static void gpio_mpsse_irq_enable(struct irq_data *irqd) |
| 513 | { |
| 514 | struct mpsse_worker *worker; |
| 515 | struct mpsse_priv *priv = irq_data_get_irq_chip_data(irqd); |
| 516 | |
| 517 | gpiochip_enable_irq(&priv->gpio, irqd->hwirq); |
| 518 | /* If no-one else was using the IRQ, enable it */ |
| 519 | if (!atomic_fetch_or(BIT(irqd->hwirq), &priv->irq_enabled)) { |
| 520 | /* |
| 521 | * Can't be devm because it uses a non-raw spinlock (illegal in |
| 522 | * this context, where a raw spinlock is held by our caller) |
| 523 | */ |
| 524 | worker = kzalloc_obj(*worker, GFP_NOWAIT); |
| 525 | if (!worker) |
| 526 | return; |
| 527 | |
| 528 | worker->priv = priv; |
| 529 | INIT_LIST_HEAD(&worker->list); |
| 530 | INIT_WORK(&worker->work, gpio_mpsse_poll); |
| 531 | |
| 532 | scoped_guard(raw_spinlock_irqsave, &priv->irq_spin) { |
| 533 | if (atomic_read(&priv->dying)) { |
| 534 | /* |
| 535 | * Cannot kfree() here: this callback |
| 536 | * runs with the IRQ descriptor's raw |
| 537 | * spinlock held and kfree() may sleep |
| 538 | * on PREEMPT_RT. The worker is not |
| 539 | * yet published, so the deferred free |
| 540 | * is unobservable. |
| 541 | */ |
| 542 | kfree_rcu(worker, rcu); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | list_add(&worker->list, &priv->workers); |
| 547 | schedule_work(&worker->work); |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | static const struct irq_chip gpio_mpsse_irq_chip = { |
| 553 | .name = "gpio-mpsse-irq", |
| 554 | .irq_enable = gpio_mpsse_irq_enable, |
| 555 | .irq_disable = gpio_mpsse_irq_disable, |
| 556 | .irq_set_type = gpio_mpsse_set_irq_type, |
| 557 | .flags = IRQCHIP_IMMUTABLE, |
| 558 | GPIOCHIP_IRQ_RESOURCE_HELPERS, |
| 559 | }; |
| 560 | |
| 561 | static void gpio_mpsse_ida_remove(void *data) |
| 562 | { |
| 563 | struct mpsse_priv *priv = data; |
| 564 | |
| 565 | ida_free(&gpio_mpsse_ida, priv->id); |
| 566 | } |
| 567 | |
| 568 | static int mpsse_init_valid_mask(struct gpio_chip *chip, |
| 569 | unsigned long *valid_mask, |
| 570 | unsigned int ngpios) |
| 571 | { |
| 572 | struct mpsse_priv *priv = gpiochip_get_data(chip); |
| 573 | |
| 574 | if (WARN_ON(priv == NULL)) |
| 575 | return -ENODEV; |
| 576 | |
| 577 | *valid_mask = priv->dir_in | priv->dir_out; |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | static void mpsse_irq_init_valid_mask(struct gpio_chip *chip, |
| 583 | unsigned long *valid_mask, |
| 584 | unsigned int ngpios) |
| 585 | { |
| 586 | struct mpsse_priv *priv = gpiochip_get_data(chip); |
| 587 | |
| 588 | if (WARN_ON(priv == NULL)) |
| 589 | return; |
| 590 | |
| 591 | /* Can only use IRQ on input capable pins */ |
| 592 | *valid_mask = priv->dir_in; |
| 593 | } |
| 594 | |
| 595 | static int gpio_mpsse_probe(struct usb_interface *interface, |
| 596 | const struct usb_device_id *id) |
| 597 | { |
| 598 | struct mpsse_priv *priv; |
| 599 | struct device *dev; |
| 600 | char *serial; |
| 601 | int err; |
| 602 | struct mpsse_quirk *quirk = (void *)id->driver_info; |
| 603 | |
| 604 | dev = &interface->dev; |
| 605 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 606 | if (!priv) |
| 607 | return -ENOMEM; |
| 608 | |
| 609 | INIT_LIST_HEAD(&priv->workers); |
| 610 | |
| 611 | priv->udev = interface_to_usbdev(interface); |
| 612 | priv->intf = interface; |
| 613 | priv->intf_id = interface->cur_altsetting->desc.bInterfaceNumber; |
| 614 | |
| 615 | priv->id = ida_alloc(&gpio_mpsse_ida, GFP_KERNEL); |
| 616 | if (priv->id < 0) |
| 617 | return priv->id; |
| 618 | |
| 619 | err = devm_add_action_or_reset(dev, gpio_mpsse_ida_remove, priv); |
| 620 | if (err) |
| 621 | return err; |
| 622 | |
| 623 | err = devm_mutex_init(dev, &priv->io_mutex); |
| 624 | if (err) |
| 625 | return err; |
| 626 | |
| 627 | err = devm_mutex_init(dev, &priv->irq_mutex); |
| 628 | if (err) |
| 629 | return err; |
| 630 | |
| 631 | err = devm_mutex_init(dev, &priv->irq_race); |
| 632 | if (err) |
| 633 | return err; |
| 634 | |
| 635 | raw_spin_lock_init(&priv->irq_spin); |
| 636 | |
| 637 | serial = priv->udev->serial; |
| 638 | if (!serial) |
| 639 | serial = "NONE"; |
| 640 | |
| 641 | priv->gpio.label = devm_kasprintf(dev, GFP_KERNEL, |
| 642 | "MPSSE%04x:%04x.%d.%d.%s", |
| 643 | id->idVendor, id->idProduct, |
| 644 | priv->intf_id, priv->id, |
| 645 | serial); |
| 646 | if (!priv->gpio.label) |
| 647 | return -ENOMEM; |
| 648 | |
| 649 | priv->gpio.owner = THIS_MODULE; |
| 650 | priv->gpio.parent = interface->usb_dev; |
| 651 | priv->gpio.get_direction = gpio_mpsse_get_direction; |
| 652 | priv->gpio.direction_input = gpio_mpsse_direction_input; |
| 653 | priv->gpio.direction_output = gpio_mpsse_direction_output; |
| 654 | priv->gpio.get = gpio_mpsse_gpio_get; |
| 655 | priv->gpio.set = gpio_mpsse_gpio_set; |
| 656 | priv->gpio.get_multiple = gpio_mpsse_get_multiple; |
| 657 | priv->gpio.set_multiple = gpio_mpsse_set_multiple; |
| 658 | priv->gpio.base = -1; |
| 659 | priv->gpio.ngpio = MPSSE_NGPIO; |
| 660 | priv->gpio.offset = priv->intf_id * priv->gpio.ngpio; |
| 661 | priv->gpio.can_sleep = 1; |
| 662 | |
| 663 | if (quirk) { |
| 664 | priv->dir_out = quirk->dir_out; |
| 665 | priv->dir_in = quirk->dir_in; |
| 666 | priv->gpio.names = quirk->names; |
| 667 | priv->gpio.init_valid_mask = mpsse_init_valid_mask; |
| 668 | } else { |
| 669 | priv->dir_in = U16_MAX; |
| 670 | priv->dir_out = U16_MAX; |
| 671 | } |
| 672 | |
| 673 | err = usb_find_common_endpoints(interface->cur_altsetting, |
| 674 | &priv->bulk_in, &priv->bulk_out, |
| 675 | NULL, NULL); |
| 676 | if (err) |
| 677 | return err; |
| 678 | |
| 679 | priv->bulk_in_buf = devm_kmalloc(dev, usb_endpoint_maxp(priv->bulk_in), |
| 680 | GFP_KERNEL); |
| 681 | if (!priv->bulk_in_buf) |
| 682 | return -ENOMEM; |
| 683 | |
| 684 | usb_set_intfdata(interface, priv); |
| 685 | |
| 686 | /* Reset mode, needed to correctly enter MPSSE mode */ |
| 687 | err = usb_control_msg(priv->udev, usb_sndctrlpipe(priv->udev, 0), |
| 688 | SET_BITMODE_REQUEST, |
| 689 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 690 | MODE_RESET, priv->intf_id + 1, NULL, 0, |
| 691 | USB_CTRL_SET_TIMEOUT); |
| 692 | if (err) |
| 693 | return err; |
| 694 | |
| 695 | /* Enter MPSSE mode */ |
| 696 | err = usb_control_msg(priv->udev, usb_sndctrlpipe(priv->udev, 0), |
| 697 | SET_BITMODE_REQUEST, |
| 698 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 699 | MODE_MPSSE, priv->intf_id + 1, NULL, 0, |
| 700 | USB_CTRL_SET_TIMEOUT); |
| 701 | if (err) |
| 702 | return err; |
| 703 | |
| 704 | gpio_irq_chip_set_chip(&priv->gpio.irq, &gpio_mpsse_irq_chip); |
| 705 | |
| 706 | priv->gpio.irq.parent_handler = NULL; |
| 707 | priv->gpio.irq.num_parents = 0; |
| 708 | priv->gpio.irq.parents = NULL; |
| 709 | priv->gpio.irq.default_type = IRQ_TYPE_NONE; |
| 710 | priv->gpio.irq.handler = handle_simple_irq; |
| 711 | priv->gpio.irq.init_valid_mask = mpsse_irq_init_valid_mask; |
| 712 | |
| 713 | err = devm_gpiochip_add_data(dev, &priv->gpio, priv); |
| 714 | if (err) |
| 715 | return err; |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | static void gpio_mpsse_disconnect(struct usb_interface *intf) |
| 721 | { |
| 722 | struct mpsse_priv *priv = usb_get_intfdata(intf); |
| 723 | |
| 724 | scoped_guard(raw_spinlock_irqsave, &priv->irq_spin) |
| 725 | atomic_set(&priv->dying, 1); |
| 726 | |
| 727 | /* |
| 728 | * Lock prevents double-free of worker from here and the teardown |
| 729 | * step at the beginning of gpio_mpsse_poll |
| 730 | */ |
| 731 | scoped_guard(mutex, &priv->irq_race) |
| 732 | gpio_mpsse_stop_all_except(priv, NULL); |
| 733 | |
| 734 | priv->intf = NULL; |
| 735 | usb_set_intfdata(intf, NULL); |
| 736 | } |
| 737 | |
| 738 | static struct usb_driver gpio_mpsse_driver = { |
| 739 | .name = "gpio-mpsse", |
| 740 | .probe = gpio_mpsse_probe, |
| 741 | .disconnect = gpio_mpsse_disconnect, |
| 742 | .id_table = gpio_mpsse_table, |
| 743 | }; |
| 744 | |
| 745 | module_usb_driver(gpio_mpsse_driver); |
| 746 | |
| 747 | MODULE_AUTHOR("Mary Strodl <mstrodl@csh.rit.edu>"); |
| 748 | MODULE_DESCRIPTION("MPSSE GPIO driver"); |
| 749 | MODULE_LICENSE("GPL"); |