| 1 | |
| 2 | |
| 3 | * Driver for onboard USB devices
|
| 4 | *
|
| 5 | * Copyright (c) 2022, Google LLC
|
| 6 |
|
| 7 |
|
| 8 | #include <linux/clk.h>
|
| 9 | #include <linux/device.h>
|
| 10 | #include <linux/export.h>
|
| 11 | #include <linux/err.h>
|
| 12 | #include <linux/gpio/consumer.h>
|
| 13 | #include <linux/init.h>
|
| 14 | #include <linux/i2c.h>
|
| 15 | #include <linux/kernel.h>
|
| 16 | #include <linux/list.h>
|
| 17 | #include <linux/module.h>
|
| 18 | #include <linux/mutex.h>
|
| 19 | #include <linux/of.h>
|
| 20 | #include <linux/of_platform.h>
|
| 21 | #include <linux/platform_device.h>
|
| 22 | #include <linux/regulator/consumer.h>
|
| 23 | #include <linux/slab.h>
|
| 24 | #include <linux/suspend.h>
|
| 25 | #include <linux/sysfs.h>
|
| 26 | #include <linux/usb.h>
|
| 27 | #include <linux/usb/hcd.h>
|
| 28 | #include <linux/usb/onboard_dev.h>
|
| 29 | #include <linux/workqueue.h>
|
| 30 |
|
| 31 | #include "onboard_usb_dev.h"
|
| 32 |
|
| 33 |
|
| 34 | #define USB5744_CMD_ATTACH 0xAA
|
| 35 | #define USB5744_CMD_ATTACH_LSB 0x56
|
| 36 | #define USB5744_CMD_CREG_ACCESS 0x99
|
| 37 | #define USB5744_CMD_CREG_ACCESS_LSB 0x37
|
| 38 | #define USB5744_CREG_MEM_ADDR 0x00
|
| 39 | #define USB5744_CREG_MEM_RD_ADDR 0x04
|
| 40 | #define USB5744_CREG_WRITE 0x00
|
| 41 | #define USB5744_CREG_READ 0x01
|
| 42 | #define USB5744_CREG_RUNTIMEFLAGS2 0x411D
|
| 43 | #define USB5744_CREG_BYPASS_UDC_SUSPEND BIT(3)
|
| 44 |
|
| 45 | static void onboard_dev_attach_usb_driver(struct work_struct *work);
|
| 46 |
|
| 47 | static struct usb_device_driver onboard_dev_usbdev_driver;
|
| 48 | static DECLARE_WORK(attach_usb_driver_work, onboard_dev_attach_usb_driver);
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 | struct usbdev_node {
|
| 53 | struct usb_device *udev;
|
| 54 | struct list_head list;
|
| 55 | };
|
| 56 |
|
| 57 | struct onboard_dev {
|
| 58 | struct regulator_bulk_data supplies[MAX_SUPPLIES];
|
| 59 | struct device *dev;
|
| 60 | const struct onboard_dev_pdata *pdata;
|
| 61 | struct gpio_desc *reset_gpio;
|
| 62 | bool always_powered_in_suspend;
|
| 63 | bool is_powered_on;
|
| 64 | bool going_away;
|
| 65 | struct list_head udev_list;
|
| 66 | struct mutex lock;
|
| 67 | struct clk *clk;
|
| 68 | };
|
| 69 |
|
| 70 | static int onboard_dev_get_regulators(struct onboard_dev *onboard_dev)
|
| 71 | {
|
| 72 | const char * const *supply_names = onboard_dev->pdata->supply_names;
|
| 73 | unsigned int num_supplies = onboard_dev->pdata->num_supplies;
|
| 74 | struct device *dev = onboard_dev->dev;
|
| 75 | unsigned int i;
|
| 76 | int err;
|
| 77 |
|
| 78 | if (num_supplies > MAX_SUPPLIES)
|
| 79 | return dev_err_probe(dev, -EINVAL, "max %d supplies supported!\n",
|
| 80 | MAX_SUPPLIES);
|
| 81 |
|
| 82 | for (i = 0; i < num_supplies; i++)
|
| 83 | onboard_dev->supplies[i].supply = supply_names[i];
|
| 84 |
|
| 85 | err = devm_regulator_bulk_get(dev, num_supplies, onboard_dev->supplies);
|
| 86 | if (err)
|
| 87 | dev_err(dev, "Failed to get regulator supplies: %pe\n",
|
| 88 | ERR_PTR(err));
|
| 89 |
|
| 90 | return err;
|
| 91 | }
|
| 92 |
|
| 93 | static int onboard_dev_power_on(struct onboard_dev *onboard_dev)
|
| 94 | {
|
| 95 | int err;
|
| 96 |
|
| 97 | err = clk_prepare_enable(onboard_dev->clk);
|
| 98 | if (err) {
|
| 99 | dev_err(onboard_dev->dev, "failed to enable clock: %pe\n",
|
| 100 | ERR_PTR(err));
|
| 101 | return err;
|
| 102 | }
|
| 103 |
|
| 104 | err = regulator_bulk_enable(onboard_dev->pdata->num_supplies,
|
| 105 | onboard_dev->supplies);
|
| 106 | if (err) {
|
| 107 | dev_err(onboard_dev->dev, "failed to enable supplies: %pe\n",
|
| 108 | ERR_PTR(err));
|
| 109 | goto disable_clk;
|
| 110 | }
|
| 111 |
|
| 112 | fsleep(onboard_dev->pdata->reset_us);
|
| 113 | gpiod_set_value_cansleep(onboard_dev->reset_gpio, 0);
|
| 114 | fsleep(onboard_dev->pdata->power_on_delay_us);
|
| 115 |
|
| 116 | onboard_dev->is_powered_on = true;
|
| 117 |
|
| 118 | return 0;
|
| 119 |
|
| 120 | disable_clk:
|
| 121 | clk_disable_unprepare(onboard_dev->clk);
|
| 122 | return err;
|
| 123 | }
|
| 124 |
|
| 125 | static int onboard_dev_power_off(struct onboard_dev *onboard_dev)
|
| 126 | {
|
| 127 | int err;
|
| 128 |
|
| 129 | gpiod_set_value_cansleep(onboard_dev->reset_gpio, 1);
|
| 130 |
|
| 131 | err = regulator_bulk_disable(onboard_dev->pdata->num_supplies,
|
| 132 | onboard_dev->supplies);
|
| 133 | if (err) {
|
| 134 | dev_err(onboard_dev->dev, "failed to disable supplies: %pe\n",
|
| 135 | ERR_PTR(err));
|
| 136 | return err;
|
| 137 | }
|
| 138 |
|
| 139 | clk_disable_unprepare(onboard_dev->clk);
|
| 140 |
|
| 141 | onboard_dev->is_powered_on = false;
|
| 142 |
|
| 143 | return 0;
|
| 144 | }
|
| 145 |
|
| 146 | static int __maybe_unused onboard_dev_suspend(struct device *dev)
|
| 147 | {
|
| 148 | struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
|
| 149 | struct usbdev_node *node;
|
| 150 | bool power_off = true;
|
| 151 |
|
| 152 | if (onboard_dev->always_powered_in_suspend)
|
| 153 | return 0;
|
| 154 |
|
| 155 | mutex_lock(&onboard_dev->lock);
|
| 156 |
|
| 157 | list_for_each_entry(node, &onboard_dev->udev_list, list) {
|
| 158 | if (!device_may_wakeup(node->udev->bus->controller))
|
| 159 | continue;
|
| 160 |
|
| 161 | if (usb_wakeup_enabled_descendants(node->udev)) {
|
| 162 | power_off = false;
|
| 163 | break;
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | mutex_unlock(&onboard_dev->lock);
|
| 168 |
|
| 169 | if (!power_off)
|
| 170 | return 0;
|
| 171 |
|
| 172 | return onboard_dev_power_off(onboard_dev);
|
| 173 | }
|
| 174 |
|
| 175 | static int __maybe_unused onboard_dev_resume(struct device *dev)
|
| 176 | {
|
| 177 | struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
|
| 178 |
|
| 179 | if (onboard_dev->is_powered_on)
|
| 180 | return 0;
|
| 181 |
|
| 182 | return onboard_dev_power_on(onboard_dev);
|
| 183 | }
|
| 184 |
|
| 185 | static inline void get_udev_link_name(const struct usb_device *udev, char *buf,
|
| 186 | size_t size)
|
| 187 | {
|
| 188 | snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev));
|
| 189 | }
|
| 190 |
|
| 191 | static int onboard_dev_add_usbdev(struct onboard_dev *onboard_dev,
|
| 192 | struct usb_device *udev)
|
| 193 | {
|
| 194 | struct usbdev_node *node;
|
| 195 | char link_name[64];
|
| 196 | int err;
|
| 197 |
|
| 198 | mutex_lock(&onboard_dev->lock);
|
| 199 |
|
| 200 | if (onboard_dev->going_away) {
|
| 201 | err = -EINVAL;
|
| 202 | goto error;
|
| 203 | }
|
| 204 |
|
| 205 | node = kzalloc_obj(*node);
|
| 206 | if (!node) {
|
| 207 | err = -ENOMEM;
|
| 208 | goto error;
|
| 209 | }
|
| 210 |
|
| 211 | node->udev = udev;
|
| 212 |
|
| 213 | list_add(&node->list, &onboard_dev->udev_list);
|
| 214 |
|
| 215 | mutex_unlock(&onboard_dev->lock);
|
| 216 |
|
| 217 | get_udev_link_name(udev, link_name, sizeof(link_name));
|
| 218 | WARN_ON(sysfs_create_link(&onboard_dev->dev->kobj, &udev->dev.kobj,
|
| 219 | link_name));
|
| 220 |
|
| 221 | return 0;
|
| 222 |
|
| 223 | error:
|
| 224 | mutex_unlock(&onboard_dev->lock);
|
| 225 |
|
| 226 | return err;
|
| 227 | }
|
| 228 |
|
| 229 | static void onboard_dev_remove_usbdev(struct onboard_dev *onboard_dev,
|
| 230 | const struct usb_device *udev)
|
| 231 | {
|
| 232 | struct usbdev_node *node;
|
| 233 | char link_name[64];
|
| 234 |
|
| 235 | get_udev_link_name(udev, link_name, sizeof(link_name));
|
| 236 | sysfs_remove_link(&onboard_dev->dev->kobj, link_name);
|
| 237 |
|
| 238 | mutex_lock(&onboard_dev->lock);
|
| 239 |
|
| 240 | list_for_each_entry(node, &onboard_dev->udev_list, list) {
|
| 241 | if (node->udev == udev) {
|
| 242 | list_del(&node->list);
|
| 243 | kfree(node);
|
| 244 | break;
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | mutex_unlock(&onboard_dev->lock);
|
| 249 | }
|
| 250 |
|
| 251 | static ssize_t always_powered_in_suspend_show(struct device *dev,
|
| 252 | struct device_attribute *attr,
|
| 253 | char *buf)
|
| 254 | {
|
| 255 | const struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
|
| 256 |
|
| 257 | return sysfs_emit(buf, "%d\n", onboard_dev->always_powered_in_suspend);
|
| 258 | }
|
| 259 |
|
| 260 | static ssize_t always_powered_in_suspend_store(struct device *dev,
|
| 261 | struct device_attribute *attr,
|
| 262 | const char *buf, size_t count)
|
| 263 | {
|
| 264 | struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
|
| 265 | bool val;
|
| 266 | int ret;
|
| 267 |
|
| 268 | ret = kstrtobool(buf, &val);
|
| 269 | if (ret < 0)
|
| 270 | return ret;
|
| 271 |
|
| 272 | onboard_dev->always_powered_in_suspend = val;
|
| 273 |
|
| 274 | return count;
|
| 275 | }
|
| 276 | static DEVICE_ATTR_RW(always_powered_in_suspend);
|
| 277 |
|
| 278 | static struct attribute *onboard_dev_attrs[] = {
|
| 279 | &dev_attr_always_powered_in_suspend.attr,
|
| 280 | NULL,
|
| 281 | };
|
| 282 |
|
| 283 | static umode_t onboard_dev_attrs_are_visible(struct kobject *kobj,
|
| 284 | struct attribute *attr,
|
| 285 | int n)
|
| 286 | {
|
| 287 | struct device *dev = kobj_to_dev(kobj);
|
| 288 | struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
|
| 289 |
|
| 290 | if (attr == &dev_attr_always_powered_in_suspend.attr &&
|
| 291 | !onboard_dev->pdata->is_hub)
|
| 292 | return 0;
|
| 293 |
|
| 294 | return attr->mode;
|
| 295 | }
|
| 296 |
|
| 297 | static const struct attribute_group onboard_dev_group = {
|
| 298 | .is_visible = onboard_dev_attrs_are_visible,
|
| 299 | .attrs = onboard_dev_attrs,
|
| 300 | };
|
| 301 | __ATTRIBUTE_GROUPS(onboard_dev);
|
| 302 |
|
| 303 |
|
| 304 | static void onboard_dev_attach_usb_driver(struct work_struct *work)
|
| 305 | {
|
| 306 | int err;
|
| 307 |
|
| 308 | err = driver_attach(&onboard_dev_usbdev_driver.driver);
|
| 309 | if (err)
|
| 310 | pr_err("Failed to attach USB driver: %pe\n", ERR_PTR(err));
|
| 311 | }
|
| 312 |
|
| 313 | #if IS_ENABLED(CONFIG_USB_ONBOARD_DEV_USB5744)
|
| 314 | static int onboard_dev_5744_i2c_read_byte(struct i2c_client *client, u16 addr, u8 *data)
|
| 315 | {
|
| 316 | struct i2c_msg msg[2];
|
| 317 | u8 rd_buf[3];
|
| 318 | int ret;
|
| 319 |
|
| 320 | u8 wr_buf[7] = {0, USB5744_CREG_MEM_ADDR, 4,
|
| 321 | USB5744_CREG_READ, 1,
|
| 322 | addr >> 8 & 0xff,
|
| 323 | addr & 0xff};
|
| 324 | msg[0].addr = client->addr;
|
| 325 | msg[0].flags = 0;
|
| 326 | msg[0].len = sizeof(wr_buf);
|
| 327 | msg[0].buf = wr_buf;
|
| 328 |
|
| 329 | ret = i2c_transfer(client->adapter, msg, 1);
|
| 330 | if (ret < 0)
|
| 331 | return ret;
|
| 332 |
|
| 333 | wr_buf[0] = USB5744_CMD_CREG_ACCESS;
|
| 334 | wr_buf[1] = USB5744_CMD_CREG_ACCESS_LSB;
|
| 335 | wr_buf[2] = 0;
|
| 336 | msg[0].len = 3;
|
| 337 |
|
| 338 | ret = i2c_transfer(client->adapter, msg, 1);
|
| 339 | if (ret < 0)
|
| 340 | return ret;
|
| 341 |
|
| 342 | wr_buf[0] = 0;
|
| 343 | wr_buf[1] = USB5744_CREG_MEM_RD_ADDR;
|
| 344 | msg[0].len = 2;
|
| 345 |
|
| 346 | msg[1].addr = client->addr;
|
| 347 | msg[1].flags = I2C_M_RD;
|
| 348 | msg[1].len = 2;
|
| 349 | msg[1].buf = rd_buf;
|
| 350 |
|
| 351 | ret = i2c_transfer(client->adapter, msg, 2);
|
| 352 | if (ret < 0)
|
| 353 | return ret;
|
| 354 | *data = rd_buf[1];
|
| 355 |
|
| 356 | return 0;
|
| 357 | }
|
| 358 |
|
| 359 | static int onboard_dev_5744_i2c_write_byte(struct i2c_client *client, u16 addr, u8 data)
|
| 360 | {
|
| 361 | struct i2c_msg msg[2];
|
| 362 | int ret;
|
| 363 |
|
| 364 | u8 wr_buf[8] = {0, USB5744_CREG_MEM_ADDR, 5,
|
| 365 | USB5744_CREG_WRITE, 1,
|
| 366 | addr >> 8 & 0xff,
|
| 367 | addr & 0xff,
|
| 368 | data};
|
| 369 | msg[0].addr = client->addr;
|
| 370 | msg[0].flags = 0;
|
| 371 | msg[0].len = sizeof(wr_buf);
|
| 372 | msg[0].buf = wr_buf;
|
| 373 |
|
| 374 | ret = i2c_transfer(client->adapter, msg, 1);
|
| 375 | if (ret < 0)
|
| 376 | return ret;
|
| 377 |
|
| 378 | msg[0].len = 3;
|
| 379 | wr_buf[0] = USB5744_CMD_CREG_ACCESS;
|
| 380 | wr_buf[1] = USB5744_CMD_CREG_ACCESS_LSB;
|
| 381 | wr_buf[2] = 0;
|
| 382 |
|
| 383 | ret = i2c_transfer(client->adapter, msg, 1);
|
| 384 | if (ret < 0)
|
| 385 | return ret;
|
| 386 |
|
| 387 | return 0;
|
| 388 | }
|
| 389 |
|
| 390 | static int onboard_dev_5744_i2c_init(struct i2c_client *client)
|
| 391 | {
|
| 392 | struct device *dev = &client->dev;
|
| 393 | int ret;
|
| 394 | u8 reg;
|
| 395 |
|
| 396 | |
| 397 | * Set BYPASS_UDC_SUSPEND bit to ensure MCU is always enabled
|
| 398 | * and ready to respond to SMBus runtime commands.
|
| 399 | * The command writes 5 bytes to memory and single data byte in
|
| 400 | * configuration register.
|
| 401 |
|
| 402 | ret = onboard_dev_5744_i2c_read_byte(client,
|
| 403 | USB5744_CREG_RUNTIMEFLAGS2, ®);
|
| 404 | if (ret)
|
| 405 | return dev_err_probe(dev, ret, "CREG_RUNTIMEFLAGS2 read failed\n");
|
| 406 |
|
| 407 | reg |= USB5744_CREG_BYPASS_UDC_SUSPEND;
|
| 408 | ret = onboard_dev_5744_i2c_write_byte(client,
|
| 409 | USB5744_CREG_RUNTIMEFLAGS2, reg);
|
| 410 | if (ret)
|
| 411 | return dev_err_probe(dev, ret, "BYPASS_UDC_SUSPEND bit configuration failed\n");
|
| 412 |
|
| 413 |
|
| 414 | ret = i2c_smbus_write_word_data(client, USB5744_CMD_ATTACH,
|
| 415 | USB5744_CMD_ATTACH_LSB);
|
| 416 | if (ret < 0)
|
| 417 | return dev_err_probe(dev, ret, "USB Attach with SMBus command failed\n");
|
| 418 |
|
| 419 | return ret;
|
| 420 | }
|
| 421 | #else
|
| 422 | static int onboard_dev_5744_i2c_init(struct i2c_client *client)
|
| 423 | {
|
| 424 | return -ENODEV;
|
| 425 | }
|
| 426 | #endif
|
| 427 |
|
| 428 | static int onboard_dev_probe(struct platform_device *pdev)
|
| 429 | {
|
| 430 | struct device *dev = &pdev->dev;
|
| 431 | struct onboard_dev *onboard_dev;
|
| 432 | struct device_node *i2c_node;
|
| 433 | int err;
|
| 434 |
|
| 435 | onboard_dev = devm_kzalloc(dev, sizeof(*onboard_dev), GFP_KERNEL);
|
| 436 | if (!onboard_dev)
|
| 437 | return -ENOMEM;
|
| 438 |
|
| 439 | onboard_dev->pdata = device_get_match_data(dev);
|
| 440 | if (!onboard_dev->pdata)
|
| 441 | return -EINVAL;
|
| 442 |
|
| 443 | if (!onboard_dev->pdata->is_hub)
|
| 444 | onboard_dev->always_powered_in_suspend = true;
|
| 445 |
|
| 446 | onboard_dev->dev = dev;
|
| 447 |
|
| 448 | err = onboard_dev_get_regulators(onboard_dev);
|
| 449 | if (err)
|
| 450 | return err;
|
| 451 |
|
| 452 | onboard_dev->clk = devm_clk_get_optional(dev, NULL);
|
| 453 | if (IS_ERR(onboard_dev->clk))
|
| 454 | return dev_err_probe(dev, PTR_ERR(onboard_dev->clk),
|
| 455 | "failed to get clock\n");
|
| 456 |
|
| 457 | onboard_dev->reset_gpio = devm_gpiod_get_optional(dev, "reset",
|
| 458 | GPIOD_OUT_HIGH);
|
| 459 | if (IS_ERR(onboard_dev->reset_gpio))
|
| 460 | return dev_err_probe(dev, PTR_ERR(onboard_dev->reset_gpio),
|
| 461 | "failed to get reset GPIO\n");
|
| 462 |
|
| 463 | mutex_init(&onboard_dev->lock);
|
| 464 | INIT_LIST_HEAD(&onboard_dev->udev_list);
|
| 465 |
|
| 466 | dev_set_drvdata(dev, onboard_dev);
|
| 467 |
|
| 468 | err = onboard_dev_power_on(onboard_dev);
|
| 469 | if (err)
|
| 470 | return err;
|
| 471 |
|
| 472 | i2c_node = of_parse_phandle(pdev->dev.of_node, "i2c-bus", 0);
|
| 473 | if (i2c_node) {
|
| 474 | struct i2c_client *client = NULL;
|
| 475 |
|
| 476 | #if IS_ENABLED(CONFIG_USB_ONBOARD_DEV_USB5744)
|
| 477 | client = of_find_i2c_device_by_node(i2c_node);
|
| 478 | #endif
|
| 479 | of_node_put(i2c_node);
|
| 480 |
|
| 481 | if (!client) {
|
| 482 | err = -EPROBE_DEFER;
|
| 483 | goto err_power_off;
|
| 484 | }
|
| 485 |
|
| 486 | if (of_device_is_compatible(pdev->dev.of_node, "usb424,2744") ||
|
| 487 | of_device_is_compatible(pdev->dev.of_node, "usb424,5744")) {
|
| 488 | err = onboard_dev_5744_i2c_init(client);
|
| 489 | onboard_dev->always_powered_in_suspend = true;
|
| 490 | }
|
| 491 |
|
| 492 | put_device(&client->dev);
|
| 493 | if (err < 0)
|
| 494 | goto err_power_off;
|
| 495 | }
|
| 496 |
|
| 497 | |
| 498 | * The USB driver might have been detached from the USB devices by
|
| 499 | * onboard_dev_remove() (e.g. through an 'unbind' by userspace),
|
| 500 | * make sure to re-attach it if needed.
|
| 501 | *
|
| 502 | * This needs to be done deferred to avoid self-deadlocks on systems
|
| 503 | * with nested onboard hubs.
|
| 504 |
|
| 505 | schedule_work(&attach_usb_driver_work);
|
| 506 |
|
| 507 | return 0;
|
| 508 |
|
| 509 | err_power_off:
|
| 510 | onboard_dev_power_off(onboard_dev);
|
| 511 | return err;
|
| 512 | }
|
| 513 |
|
| 514 | static void onboard_dev_remove(struct platform_device *pdev)
|
| 515 | {
|
| 516 | struct onboard_dev *onboard_dev = dev_get_drvdata(&pdev->dev);
|
| 517 | struct usbdev_node *node;
|
| 518 | struct usb_device *udev;
|
| 519 |
|
| 520 | onboard_dev->going_away = true;
|
| 521 |
|
| 522 | mutex_lock(&onboard_dev->lock);
|
| 523 |
|
| 524 |
|
| 525 | while (!list_empty(&onboard_dev->udev_list)) {
|
| 526 | node = list_first_entry(&onboard_dev->udev_list,
|
| 527 | struct usbdev_node, list);
|
| 528 | udev = node->udev;
|
| 529 |
|
| 530 | |
| 531 | * Unbinding the driver will call onboard_dev_remove_usbdev(),
|
| 532 | * which acquires onboard_dev->lock. We must release the lock
|
| 533 | * first.
|
| 534 |
|
| 535 | get_device(&udev->dev);
|
| 536 | mutex_unlock(&onboard_dev->lock);
|
| 537 | device_release_driver(&udev->dev);
|
| 538 | put_device(&udev->dev);
|
| 539 | mutex_lock(&onboard_dev->lock);
|
| 540 | }
|
| 541 |
|
| 542 | mutex_unlock(&onboard_dev->lock);
|
| 543 |
|
| 544 | onboard_dev_power_off(onboard_dev);
|
| 545 | }
|
| 546 |
|
| 547 | MODULE_DEVICE_TABLE(of, onboard_dev_match);
|
| 548 |
|
| 549 | static const struct dev_pm_ops __maybe_unused onboard_dev_pm_ops = {
|
| 550 | SET_LATE_SYSTEM_SLEEP_PM_OPS(onboard_dev_suspend, onboard_dev_resume)
|
| 551 | };
|
| 552 |
|
| 553 | static struct platform_driver onboard_dev_driver = {
|
| 554 | .probe = onboard_dev_probe,
|
| 555 | .remove = onboard_dev_remove,
|
| 556 |
|
| 557 | .driver = {
|
| 558 | .name = "onboard-usb-dev",
|
| 559 | .of_match_table = onboard_dev_match,
|
| 560 | .pm = pm_ptr(&onboard_dev_pm_ops),
|
| 561 | .dev_groups = onboard_dev_groups,
|
| 562 | },
|
| 563 | };
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 | #define VENDOR_ID_BISON 0x5986
|
| 568 | #define VENDOR_ID_CORECHIPS 0x3431
|
| 569 | #define VENDOR_ID_CYPRESS 0x04b4
|
| 570 | #define VENDOR_ID_GENESYS 0x05e3
|
| 571 | #define VENDOR_ID_MICROCHIP 0x0424
|
| 572 | #define VENDOR_ID_PARADE 0x1da0
|
| 573 | #define VENDOR_ID_REALTEK 0x0bda
|
| 574 | #define VENDOR_ID_TERMINUS 0x1a40
|
| 575 | #define VENDOR_ID_TI 0x0451
|
| 576 | #define VENDOR_ID_VIA 0x2109
|
| 577 | #define VENDOR_ID_XMOS 0x20B1
|
| 578 |
|
| 579 | |
| 580 | * Returns the onboard_dev platform device that is associated with the USB
|
| 581 | * device passed as parameter.
|
| 582 |
|
| 583 | static struct onboard_dev *_find_onboard_dev(struct device *dev)
|
| 584 | {
|
| 585 | struct platform_device *pdev;
|
| 586 | struct device_node *np;
|
| 587 | struct onboard_dev *onboard_dev;
|
| 588 |
|
| 589 | pdev = of_find_device_by_node(dev->of_node);
|
| 590 | if (!pdev) {
|
| 591 | np = of_parse_phandle(dev->of_node, "peer-hub", 0);
|
| 592 | if (!np) {
|
| 593 | dev_err(dev, "failed to find device node for peer hub\n");
|
| 594 | return ERR_PTR(-EINVAL);
|
| 595 | }
|
| 596 |
|
| 597 | pdev = of_find_device_by_node(np);
|
| 598 | of_node_put(np);
|
| 599 |
|
| 600 | if (!pdev)
|
| 601 | return ERR_PTR(-ENODEV);
|
| 602 | }
|
| 603 |
|
| 604 | onboard_dev = dev_get_drvdata(&pdev->dev);
|
| 605 | put_device(&pdev->dev);
|
| 606 |
|
| 607 | |
| 608 | * The presence of drvdata indicates that the platform driver finished
|
| 609 | * probing. This handles the case where (conceivably) we could be
|
| 610 | * running at the exact same time as the platform driver's probe. If
|
| 611 | * we detect the race we request probe deferral and we'll come back and
|
| 612 | * try again.
|
| 613 |
|
| 614 | if (!onboard_dev)
|
| 615 | return ERR_PTR(-EPROBE_DEFER);
|
| 616 |
|
| 617 | return onboard_dev;
|
| 618 | }
|
| 619 |
|
| 620 | static bool onboard_dev_usbdev_match(struct usb_device *udev)
|
| 621 | {
|
| 622 |
|
| 623 | return !!udev->dev.of_node;
|
| 624 | }
|
| 625 |
|
| 626 | static int onboard_dev_usbdev_probe(struct usb_device *udev)
|
| 627 | {
|
| 628 | struct device *dev = &udev->dev;
|
| 629 | struct onboard_dev *onboard_dev;
|
| 630 | int err;
|
| 631 |
|
| 632 | onboard_dev = _find_onboard_dev(dev);
|
| 633 | if (IS_ERR(onboard_dev))
|
| 634 | return PTR_ERR(onboard_dev);
|
| 635 |
|
| 636 | dev_set_drvdata(dev, onboard_dev);
|
| 637 |
|
| 638 | err = onboard_dev_add_usbdev(onboard_dev, udev);
|
| 639 | if (err)
|
| 640 | return err;
|
| 641 |
|
| 642 | return 0;
|
| 643 | }
|
| 644 |
|
| 645 | static void onboard_dev_usbdev_disconnect(struct usb_device *udev)
|
| 646 | {
|
| 647 | struct onboard_dev *onboard_dev = dev_get_drvdata(&udev->dev);
|
| 648 |
|
| 649 | onboard_dev_remove_usbdev(onboard_dev, udev);
|
| 650 | }
|
| 651 |
|
| 652 | static const struct usb_device_id onboard_dev_id_table[] = {
|
| 653 | { USB_DEVICE(VENDOR_ID_BISON, 0x1198) },
|
| 654 | { USB_DEVICE(VENDOR_ID_CORECHIPS, 0x6241) },
|
| 655 | { USB_DEVICE(VENDOR_ID_CORECHIPS, 0x6341) },
|
| 656 | { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6500) },
|
| 657 | { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6502) },
|
| 658 | { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6503) },
|
| 659 | { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6504) },
|
| 660 | { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6506) },
|
| 661 | { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6507) },
|
| 662 | { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6508) },
|
| 663 | { USB_DEVICE(VENDOR_ID_CYPRESS, 0x650a) },
|
| 664 | { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6570) },
|
| 665 | { USB_DEVICE(VENDOR_ID_GENESYS, 0x0608) },
|
| 666 | { USB_DEVICE(VENDOR_ID_GENESYS, 0x0610) },
|
| 667 | { USB_DEVICE(VENDOR_ID_GENESYS, 0x0620) },
|
| 668 | { USB_DEVICE(VENDOR_ID_GENESYS, 0x0625) },
|
| 669 | { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2412) },
|
| 670 | { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2514) },
|
| 671 | { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2517) },
|
| 672 | { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2744) },
|
| 673 | { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x5744) },
|
| 674 | { USB_DEVICE(VENDOR_ID_PARADE, 0x5511) },
|
| 675 | { USB_DEVICE(VENDOR_ID_PARADE, 0x55a1) },
|
| 676 | { USB_DEVICE(VENDOR_ID_REALTEK, 0x0411) },
|
| 677 | { USB_DEVICE(VENDOR_ID_REALTEK, 0x5411) },
|
| 678 | { USB_DEVICE(VENDOR_ID_REALTEK, 0x0414) },
|
| 679 | { USB_DEVICE(VENDOR_ID_REALTEK, 0x5414) },
|
| 680 | { USB_DEVICE(VENDOR_ID_REALTEK, 0x0179) },
|
| 681 | { USB_DEVICE(VENDOR_ID_TERMINUS, 0x0101) },
|
| 682 | { USB_DEVICE(VENDOR_ID_TI, 0x8025) },
|
| 683 | { USB_DEVICE(VENDOR_ID_TI, 0x8027) },
|
| 684 | { USB_DEVICE(VENDOR_ID_TI, 0x8140) },
|
| 685 | { USB_DEVICE(VENDOR_ID_TI, 0x8142) },
|
| 686 | { USB_DEVICE(VENDOR_ID_TI, 0x8440) },
|
| 687 | { USB_DEVICE(VENDOR_ID_TI, 0x8442) },
|
| 688 | { USB_DEVICE(VENDOR_ID_VIA, 0x0817) },
|
| 689 | { USB_DEVICE(VENDOR_ID_VIA, 0x2817) },
|
| 690 | { USB_DEVICE(VENDOR_ID_XMOS, 0x0013) },
|
| 691 | {}
|
| 692 | };
|
| 693 | MODULE_DEVICE_TABLE(usb, onboard_dev_id_table);
|
| 694 |
|
| 695 | static struct usb_device_driver onboard_dev_usbdev_driver = {
|
| 696 | .name = "onboard-usb-dev",
|
| 697 | .match = onboard_dev_usbdev_match,
|
| 698 | .probe = onboard_dev_usbdev_probe,
|
| 699 | .disconnect = onboard_dev_usbdev_disconnect,
|
| 700 | .generic_subclass = 1,
|
| 701 | .supports_autosuspend = 1,
|
| 702 | .id_table = onboard_dev_id_table,
|
| 703 | };
|
| 704 |
|
| 705 | static int __init onboard_dev_init(void)
|
| 706 | {
|
| 707 | int ret;
|
| 708 |
|
| 709 | ret = usb_register_device_driver(&onboard_dev_usbdev_driver, THIS_MODULE);
|
| 710 | if (ret)
|
| 711 | return ret;
|
| 712 |
|
| 713 | ret = platform_driver_register(&onboard_dev_driver);
|
| 714 | if (ret)
|
| 715 | usb_deregister_device_driver(&onboard_dev_usbdev_driver);
|
| 716 |
|
| 717 | return ret;
|
| 718 | }
|
| 719 | module_init(onboard_dev_init);
|
| 720 |
|
| 721 | static void __exit onboard_dev_exit(void)
|
| 722 | {
|
| 723 | usb_deregister_device_driver(&onboard_dev_usbdev_driver);
|
| 724 | platform_driver_unregister(&onboard_dev_driver);
|
| 725 |
|
| 726 | cancel_work_sync(&attach_usb_driver_work);
|
| 727 | }
|
| 728 | module_exit(onboard_dev_exit);
|
| 729 |
|
| 730 | MODULE_AUTHOR("Matthias Kaehlcke <[email protected]>");
|
| 731 | MODULE_DESCRIPTION("Driver for discrete onboard USB devices");
|
| 732 | MODULE_LICENSE("GPL v2");
|