| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Sony MemoryStick support |
| 4 | * |
| 5 | * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com> |
| 6 | * |
| 7 | * Special thanks to Carlos Corbacho for providing various MemoryStick cards |
| 8 | * that made this driver possible. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/memstick.h> |
| 12 | #include <linux/idr.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/pm_runtime.h> |
| 18 | |
| 19 | #define DRIVER_NAME "memstick" |
| 20 | |
| 21 | static unsigned int cmd_retries = 3; |
| 22 | module_param(cmd_retries, uint, 0644); |
| 23 | |
| 24 | static struct workqueue_struct *workqueue; |
| 25 | static DEFINE_IDR(memstick_host_idr); |
| 26 | static DEFINE_SPINLOCK(memstick_host_lock); |
| 27 | |
| 28 | static int memstick_dev_match(struct memstick_dev *card, |
| 29 | const struct memstick_device_id *id) |
| 30 | { |
| 31 | if (id->match_flags & MEMSTICK_MATCH_ALL) { |
| 32 | if ((id->type == card->id.type) |
| 33 | && (id->category == card->id.category) |
| 34 | && (id->class == card->id.class)) |
| 35 | return 1; |
| 36 | } |
| 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static int memstick_bus_match(struct device *dev, const struct device_driver *drv) |
| 42 | { |
| 43 | struct memstick_dev *card = container_of(dev, struct memstick_dev, |
| 44 | dev); |
| 45 | const struct memstick_driver *ms_drv = container_of_const(drv, struct memstick_driver, |
| 46 | driver); |
| 47 | const struct memstick_device_id *ids = ms_drv->id_table; |
| 48 | |
| 49 | if (ids) { |
| 50 | while (ids->match_flags) { |
| 51 | if (memstick_dev_match(card, id: ids)) |
| 52 | return 1; |
| 53 | ++ids; |
| 54 | } |
| 55 | } |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int memstick_uevent(const struct device *dev, struct kobj_uevent_env *env) |
| 60 | { |
| 61 | const struct memstick_dev *card = container_of_const(dev, struct memstick_dev, |
| 62 | dev); |
| 63 | |
| 64 | if (add_uevent_var(env, format: "MEMSTICK_TYPE=%02X" , card->id.type)) |
| 65 | return -ENOMEM; |
| 66 | |
| 67 | if (add_uevent_var(env, format: "MEMSTICK_CATEGORY=%02X" , card->id.category)) |
| 68 | return -ENOMEM; |
| 69 | |
| 70 | if (add_uevent_var(env, format: "MEMSTICK_CLASS=%02X" , card->id.class)) |
| 71 | return -ENOMEM; |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int memstick_device_probe(struct device *dev) |
| 77 | { |
| 78 | struct memstick_dev *card = container_of(dev, struct memstick_dev, |
| 79 | dev); |
| 80 | struct memstick_driver *drv = container_of(dev->driver, |
| 81 | struct memstick_driver, |
| 82 | driver); |
| 83 | int rc = -ENODEV; |
| 84 | |
| 85 | if (dev->driver && drv->probe) { |
| 86 | rc = drv->probe(card); |
| 87 | if (!rc) |
| 88 | get_device(dev); |
| 89 | } |
| 90 | return rc; |
| 91 | } |
| 92 | |
| 93 | static void memstick_device_remove(struct device *dev) |
| 94 | { |
| 95 | struct memstick_dev *card = container_of(dev, struct memstick_dev, |
| 96 | dev); |
| 97 | struct memstick_driver *drv = container_of(dev->driver, |
| 98 | struct memstick_driver, |
| 99 | driver); |
| 100 | |
| 101 | if (dev->driver && drv->remove) { |
| 102 | drv->remove(card); |
| 103 | card->dev.driver = NULL; |
| 104 | } |
| 105 | |
| 106 | put_device(dev); |
| 107 | } |
| 108 | |
| 109 | #ifdef CONFIG_PM |
| 110 | |
| 111 | static int memstick_device_suspend(struct device *dev, pm_message_t state) |
| 112 | { |
| 113 | struct memstick_dev *card = container_of(dev, struct memstick_dev, |
| 114 | dev); |
| 115 | struct memstick_driver *drv = container_of(dev->driver, |
| 116 | struct memstick_driver, |
| 117 | driver); |
| 118 | |
| 119 | if (dev->driver && drv->suspend) |
| 120 | return drv->suspend(card, state); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int memstick_device_resume(struct device *dev) |
| 125 | { |
| 126 | struct memstick_dev *card = container_of(dev, struct memstick_dev, |
| 127 | dev); |
| 128 | struct memstick_driver *drv = container_of(dev->driver, |
| 129 | struct memstick_driver, |
| 130 | driver); |
| 131 | |
| 132 | if (dev->driver && drv->resume) |
| 133 | return drv->resume(card); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | #else |
| 138 | |
| 139 | #define memstick_device_suspend NULL |
| 140 | #define memstick_device_resume NULL |
| 141 | |
| 142 | #endif /* CONFIG_PM */ |
| 143 | |
| 144 | #define MEMSTICK_ATTR(name, format) \ |
| 145 | static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \ |
| 146 | char *buf) \ |
| 147 | { \ |
| 148 | struct memstick_dev *card = container_of(dev, struct memstick_dev, \ |
| 149 | dev); \ |
| 150 | return sprintf(buf, format, card->id.name); \ |
| 151 | } \ |
| 152 | static DEVICE_ATTR_RO(name); |
| 153 | |
| 154 | MEMSTICK_ATTR(type, "%02X" ); |
| 155 | MEMSTICK_ATTR(category, "%02X" ); |
| 156 | MEMSTICK_ATTR(class, "%02X" ); |
| 157 | |
| 158 | static struct attribute *memstick_dev_attrs[] = { |
| 159 | &dev_attr_type.attr, |
| 160 | &dev_attr_category.attr, |
| 161 | &dev_attr_class.attr, |
| 162 | NULL, |
| 163 | }; |
| 164 | ATTRIBUTE_GROUPS(memstick_dev); |
| 165 | |
| 166 | static const struct bus_type memstick_bus_type = { |
| 167 | .name = "memstick" , |
| 168 | .dev_groups = memstick_dev_groups, |
| 169 | .match = memstick_bus_match, |
| 170 | .uevent = memstick_uevent, |
| 171 | .probe = memstick_device_probe, |
| 172 | .remove = memstick_device_remove, |
| 173 | .suspend = memstick_device_suspend, |
| 174 | .resume = memstick_device_resume |
| 175 | }; |
| 176 | |
| 177 | static void memstick_free(struct device *dev) |
| 178 | { |
| 179 | struct memstick_host *host = container_of(dev, struct memstick_host, |
| 180 | dev); |
| 181 | kfree(objp: host); |
| 182 | } |
| 183 | |
| 184 | static struct class memstick_host_class = { |
| 185 | .name = "memstick_host" , |
| 186 | .dev_release = memstick_free |
| 187 | }; |
| 188 | |
| 189 | static void memstick_free_card(struct device *dev) |
| 190 | { |
| 191 | struct memstick_dev *card = container_of(dev, struct memstick_dev, |
| 192 | dev); |
| 193 | kfree(objp: card); |
| 194 | } |
| 195 | |
| 196 | static int memstick_dummy_check(struct memstick_dev *card) |
| 197 | { |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * memstick_detect_change - schedule media detection on memstick host |
| 203 | * @host: host to use |
| 204 | */ |
| 205 | void memstick_detect_change(struct memstick_host *host) |
| 206 | { |
| 207 | queue_work(wq: workqueue, work: &host->media_checker); |
| 208 | } |
| 209 | EXPORT_SYMBOL(memstick_detect_change); |
| 210 | |
| 211 | /** |
| 212 | * memstick_next_req - called by host driver to obtain next request to process |
| 213 | * @host: host to use |
| 214 | * @mrq: pointer to stick the request to |
| 215 | * |
| 216 | * Host calls this function from idle state (*mrq == NULL) or after finishing |
| 217 | * previous request (*mrq should point to it). If previous request was |
| 218 | * unsuccessful, it is retried for predetermined number of times. |
| 219 | * |
| 220 | * Returns: value of 0 means that new request was assigned to the host. |
| 221 | * Otherwise a negative error code is returned. |
| 222 | */ |
| 223 | int memstick_next_req(struct memstick_host *host, struct memstick_request **mrq) |
| 224 | { |
| 225 | int rc = -ENXIO; |
| 226 | |
| 227 | if ((*mrq) && (*mrq)->error && host->retries) { |
| 228 | (*mrq)->error = rc; |
| 229 | host->retries--; |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | if (host->card && host->card->next_request) |
| 234 | rc = host->card->next_request(host->card, mrq); |
| 235 | |
| 236 | if (!rc) |
| 237 | host->retries = cmd_retries > 1 ? cmd_retries - 1 : 1; |
| 238 | else |
| 239 | *mrq = NULL; |
| 240 | |
| 241 | return rc; |
| 242 | } |
| 243 | EXPORT_SYMBOL(memstick_next_req); |
| 244 | |
| 245 | /** |
| 246 | * memstick_new_req - notify the host that some requests are pending |
| 247 | * @host: host to use |
| 248 | */ |
| 249 | void memstick_new_req(struct memstick_host *host) |
| 250 | { |
| 251 | if (host->card) { |
| 252 | host->retries = cmd_retries; |
| 253 | reinit_completion(x: &host->card->mrq_complete); |
| 254 | host->request(host); |
| 255 | } |
| 256 | } |
| 257 | EXPORT_SYMBOL(memstick_new_req); |
| 258 | |
| 259 | /** |
| 260 | * memstick_init_req_sg - set request fields needed for bulk data transfer |
| 261 | * @mrq: request to use |
| 262 | * @tpc: memstick Transport Protocol Command |
| 263 | * @sg: TPC argument |
| 264 | */ |
| 265 | void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc, |
| 266 | const struct scatterlist *sg) |
| 267 | { |
| 268 | mrq->tpc = tpc; |
| 269 | if (tpc & 8) |
| 270 | mrq->data_dir = WRITE; |
| 271 | else |
| 272 | mrq->data_dir = READ; |
| 273 | |
| 274 | mrq->sg = *sg; |
| 275 | mrq->long_data = 1; |
| 276 | |
| 277 | if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD) |
| 278 | mrq->need_card_int = 1; |
| 279 | else |
| 280 | mrq->need_card_int = 0; |
| 281 | } |
| 282 | EXPORT_SYMBOL(memstick_init_req_sg); |
| 283 | |
| 284 | /** |
| 285 | * memstick_init_req - set request fields needed for short data transfer |
| 286 | * @mrq: request to use |
| 287 | * @tpc: memstick Transport Protocol Command |
| 288 | * @buf: TPC argument buffer |
| 289 | * @length: TPC argument size |
| 290 | * |
| 291 | * The intended use of this function (transfer of data items several bytes |
| 292 | * in size) allows us to just copy the value between request structure and |
| 293 | * user supplied buffer. |
| 294 | */ |
| 295 | void memstick_init_req(struct memstick_request *mrq, unsigned char tpc, |
| 296 | const void *buf, size_t length) |
| 297 | { |
| 298 | mrq->tpc = tpc; |
| 299 | if (tpc & 8) |
| 300 | mrq->data_dir = WRITE; |
| 301 | else |
| 302 | mrq->data_dir = READ; |
| 303 | |
| 304 | mrq->data_len = length > sizeof(mrq->data) ? sizeof(mrq->data) : length; |
| 305 | if (mrq->data_dir == WRITE) |
| 306 | memcpy(mrq->data, buf, mrq->data_len); |
| 307 | |
| 308 | mrq->long_data = 0; |
| 309 | |
| 310 | if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD) |
| 311 | mrq->need_card_int = 1; |
| 312 | else |
| 313 | mrq->need_card_int = 0; |
| 314 | } |
| 315 | EXPORT_SYMBOL(memstick_init_req); |
| 316 | |
| 317 | /* |
| 318 | * Functions prefixed with "h_" are protocol callbacks. They can be called from |
| 319 | * interrupt context. Return value of 0 means that request processing is still |
| 320 | * ongoing, while special error value of -EAGAIN means that current request is |
| 321 | * finished (and request processor should come back some time later). |
| 322 | */ |
| 323 | |
| 324 | static int h_memstick_read_dev_id(struct memstick_dev *card, |
| 325 | struct memstick_request **mrq) |
| 326 | { |
| 327 | struct ms_id_register id_reg = {}; |
| 328 | |
| 329 | if (!(*mrq)) { |
| 330 | memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, &id_reg, |
| 331 | sizeof(struct ms_id_register)); |
| 332 | *mrq = &card->current_mrq; |
| 333 | return 0; |
| 334 | } |
| 335 | if (!(*mrq)->error) { |
| 336 | memcpy(&id_reg, (*mrq)->data, sizeof(id_reg)); |
| 337 | card->id.match_flags = MEMSTICK_MATCH_ALL; |
| 338 | card->id.type = id_reg.type; |
| 339 | card->id.category = id_reg.category; |
| 340 | card->id.class = id_reg.class; |
| 341 | dev_dbg(&card->dev, "if_mode = %02x\n" , id_reg.if_mode); |
| 342 | } |
| 343 | complete(&card->mrq_complete); |
| 344 | return -EAGAIN; |
| 345 | } |
| 346 | |
| 347 | static int h_memstick_set_rw_addr(struct memstick_dev *card, |
| 348 | struct memstick_request **mrq) |
| 349 | { |
| 350 | if (!(*mrq)) { |
| 351 | memstick_init_req(&card->current_mrq, MS_TPC_SET_RW_REG_ADRS, |
| 352 | (char *)&card->reg_addr, |
| 353 | sizeof(card->reg_addr)); |
| 354 | *mrq = &card->current_mrq; |
| 355 | return 0; |
| 356 | } else { |
| 357 | complete(&card->mrq_complete); |
| 358 | return -EAGAIN; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to |
| 364 | * complete |
| 365 | * @card: media device to use |
| 366 | * |
| 367 | * Returns: error setting for the current request |
| 368 | */ |
| 369 | int memstick_set_rw_addr(struct memstick_dev *card) |
| 370 | { |
| 371 | card->next_request = h_memstick_set_rw_addr; |
| 372 | memstick_new_req(card->host); |
| 373 | if (!wait_for_completion_timeout(x: &card->mrq_complete, |
| 374 | timeout: msecs_to_jiffies(m: 500))) |
| 375 | card->current_mrq.error = -ETIMEDOUT; |
| 376 | |
| 377 | return card->current_mrq.error; |
| 378 | } |
| 379 | EXPORT_SYMBOL(memstick_set_rw_addr); |
| 380 | |
| 381 | static struct memstick_dev *memstick_alloc_card(struct memstick_host *host) |
| 382 | { |
| 383 | struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev), |
| 384 | GFP_KERNEL); |
| 385 | struct memstick_dev *old_card = host->card; |
| 386 | struct ms_id_register id_reg; |
| 387 | |
| 388 | if (card) { |
| 389 | card->host = host; |
| 390 | dev_set_name(dev: &card->dev, name: "%s" , dev_name(dev: &host->dev)); |
| 391 | card->dev.parent = &host->dev; |
| 392 | card->dev.bus = &memstick_bus_type; |
| 393 | card->dev.release = memstick_free_card; |
| 394 | card->check = memstick_dummy_check; |
| 395 | |
| 396 | card->reg_addr.r_offset = offsetof(struct ms_register, id); |
| 397 | card->reg_addr.r_length = sizeof(id_reg); |
| 398 | card->reg_addr.w_offset = offsetof(struct ms_register, id); |
| 399 | card->reg_addr.w_length = sizeof(id_reg); |
| 400 | |
| 401 | init_completion(x: &card->mrq_complete); |
| 402 | |
| 403 | host->card = card; |
| 404 | if (memstick_set_rw_addr(card)) |
| 405 | goto err_out; |
| 406 | |
| 407 | card->next_request = h_memstick_read_dev_id; |
| 408 | memstick_new_req(host); |
| 409 | if (!wait_for_completion_timeout(x: &card->mrq_complete, |
| 410 | timeout: msecs_to_jiffies(m: 500))) |
| 411 | card->current_mrq.error = -ETIMEDOUT; |
| 412 | |
| 413 | if (card->current_mrq.error) |
| 414 | goto err_out; |
| 415 | } |
| 416 | host->card = old_card; |
| 417 | return card; |
| 418 | err_out: |
| 419 | host->card = old_card; |
| 420 | kfree_const(x: card->dev.kobj.name); |
| 421 | kfree(objp: card); |
| 422 | return NULL; |
| 423 | } |
| 424 | |
| 425 | static int memstick_power_on(struct memstick_host *host) |
| 426 | { |
| 427 | int rc = host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON); |
| 428 | |
| 429 | if (!rc) |
| 430 | rc = host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL); |
| 431 | |
| 432 | return rc; |
| 433 | } |
| 434 | |
| 435 | static void memstick_check(struct work_struct *work) |
| 436 | { |
| 437 | struct memstick_host *host = container_of(work, struct memstick_host, |
| 438 | media_checker); |
| 439 | struct memstick_dev *card; |
| 440 | |
| 441 | dev_dbg(&host->dev, "memstick_check started\n" ); |
| 442 | pm_runtime_get_noresume(dev: host->dev.parent); |
| 443 | mutex_lock(&host->lock); |
| 444 | if (!host->card) { |
| 445 | if (memstick_power_on(host)) |
| 446 | goto out_power_off; |
| 447 | } else if (host->card->stop) |
| 448 | host->card->stop(host->card); |
| 449 | |
| 450 | if (host->removing) |
| 451 | goto out_power_off; |
| 452 | |
| 453 | card = memstick_alloc_card(host); |
| 454 | |
| 455 | if (!card) { |
| 456 | if (host->card) { |
| 457 | device_unregister(dev: &host->card->dev); |
| 458 | host->card = NULL; |
| 459 | } |
| 460 | } else { |
| 461 | dev_dbg(&host->dev, "new card %02x, %02x, %02x\n" , |
| 462 | card->id.type, card->id.category, card->id.class); |
| 463 | if (host->card) { |
| 464 | if (memstick_set_rw_addr(host->card) |
| 465 | || !memstick_dev_match(card: host->card, id: &card->id) |
| 466 | || !(host->card->check(host->card))) { |
| 467 | device_unregister(dev: &host->card->dev); |
| 468 | host->card = NULL; |
| 469 | } else if (host->card->start) |
| 470 | host->card->start(host->card); |
| 471 | } |
| 472 | |
| 473 | if (!host->card) { |
| 474 | host->card = card; |
| 475 | if (device_register(dev: &card->dev)) { |
| 476 | put_device(dev: &card->dev); |
| 477 | host->card = NULL; |
| 478 | } |
| 479 | } else { |
| 480 | kfree_const(x: card->dev.kobj.name); |
| 481 | kfree(objp: card); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | out_power_off: |
| 486 | if (!host->card) |
| 487 | host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF); |
| 488 | |
| 489 | mutex_unlock(lock: &host->lock); |
| 490 | pm_runtime_put(dev: host->dev.parent); |
| 491 | dev_dbg(&host->dev, "memstick_check finished\n" ); |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * memstick_alloc_host - allocate a memstick_host structure |
| 496 | * @extra: size of the user private data to allocate |
| 497 | * @dev: parent device of the host |
| 498 | * |
| 499 | * Returns: %NULL on failure or the allocated &memstick_host pointer on success |
| 500 | */ |
| 501 | struct memstick_host *memstick_alloc_host(unsigned int , |
| 502 | struct device *dev) |
| 503 | { |
| 504 | struct memstick_host *host; |
| 505 | |
| 506 | host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL); |
| 507 | if (host) { |
| 508 | mutex_init(&host->lock); |
| 509 | INIT_WORK(&host->media_checker, memstick_check); |
| 510 | host->dev.class = &memstick_host_class; |
| 511 | host->dev.parent = dev; |
| 512 | device_initialize(dev: &host->dev); |
| 513 | } |
| 514 | return host; |
| 515 | } |
| 516 | EXPORT_SYMBOL(memstick_alloc_host); |
| 517 | |
| 518 | /** |
| 519 | * memstick_add_host - start request processing on memstick host |
| 520 | * @host: host to use |
| 521 | * |
| 522 | * Returns: %0 on success or a negative error code on failure |
| 523 | */ |
| 524 | int memstick_add_host(struct memstick_host *host) |
| 525 | { |
| 526 | int rc; |
| 527 | |
| 528 | idr_preload(GFP_KERNEL); |
| 529 | spin_lock(lock: &memstick_host_lock); |
| 530 | |
| 531 | rc = idr_alloc(&memstick_host_idr, ptr: host, start: 0, end: 0, GFP_NOWAIT); |
| 532 | if (rc >= 0) |
| 533 | host->id = rc; |
| 534 | |
| 535 | spin_unlock(lock: &memstick_host_lock); |
| 536 | idr_preload_end(); |
| 537 | if (rc < 0) |
| 538 | return rc; |
| 539 | |
| 540 | dev_set_name(dev: &host->dev, name: "memstick%u" , host->id); |
| 541 | |
| 542 | rc = device_add(dev: &host->dev); |
| 543 | if (rc) { |
| 544 | spin_lock(lock: &memstick_host_lock); |
| 545 | idr_remove(&memstick_host_idr, id: host->id); |
| 546 | spin_unlock(lock: &memstick_host_lock); |
| 547 | return rc; |
| 548 | } |
| 549 | |
| 550 | host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF); |
| 551 | memstick_detect_change(host); |
| 552 | return 0; |
| 553 | } |
| 554 | EXPORT_SYMBOL(memstick_add_host); |
| 555 | |
| 556 | /** |
| 557 | * memstick_remove_host - stop request processing on memstick host |
| 558 | * @host: host to use |
| 559 | */ |
| 560 | void memstick_remove_host(struct memstick_host *host) |
| 561 | { |
| 562 | flush_workqueue(workqueue); |
| 563 | mutex_lock(&host->lock); |
| 564 | if (host->card) |
| 565 | device_unregister(dev: &host->card->dev); |
| 566 | host->card = NULL; |
| 567 | host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF); |
| 568 | mutex_unlock(lock: &host->lock); |
| 569 | |
| 570 | spin_lock(lock: &memstick_host_lock); |
| 571 | idr_remove(&memstick_host_idr, id: host->id); |
| 572 | spin_unlock(lock: &memstick_host_lock); |
| 573 | device_del(dev: &host->dev); |
| 574 | } |
| 575 | EXPORT_SYMBOL(memstick_remove_host); |
| 576 | |
| 577 | /** |
| 578 | * memstick_free_host - free memstick host |
| 579 | * @host: host to use |
| 580 | */ |
| 581 | void memstick_free_host(struct memstick_host *host) |
| 582 | { |
| 583 | mutex_destroy(lock: &host->lock); |
| 584 | put_device(dev: &host->dev); |
| 585 | } |
| 586 | EXPORT_SYMBOL(memstick_free_host); |
| 587 | |
| 588 | /** |
| 589 | * memstick_suspend_host - notify bus driver of host suspension |
| 590 | * @host: host to use |
| 591 | */ |
| 592 | void memstick_suspend_host(struct memstick_host *host) |
| 593 | { |
| 594 | mutex_lock(&host->lock); |
| 595 | host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF); |
| 596 | mutex_unlock(lock: &host->lock); |
| 597 | } |
| 598 | EXPORT_SYMBOL(memstick_suspend_host); |
| 599 | |
| 600 | /** |
| 601 | * memstick_resume_host - notify bus driver of host resumption |
| 602 | * @host: host to use |
| 603 | */ |
| 604 | void memstick_resume_host(struct memstick_host *host) |
| 605 | { |
| 606 | int rc = 0; |
| 607 | |
| 608 | mutex_lock(&host->lock); |
| 609 | if (host->card) |
| 610 | rc = memstick_power_on(host); |
| 611 | mutex_unlock(lock: &host->lock); |
| 612 | |
| 613 | if (!rc) |
| 614 | memstick_detect_change(host); |
| 615 | } |
| 616 | EXPORT_SYMBOL(memstick_resume_host); |
| 617 | |
| 618 | int memstick_register_driver(struct memstick_driver *drv) |
| 619 | { |
| 620 | drv->driver.bus = &memstick_bus_type; |
| 621 | |
| 622 | return driver_register(drv: &drv->driver); |
| 623 | } |
| 624 | EXPORT_SYMBOL(memstick_register_driver); |
| 625 | |
| 626 | void memstick_unregister_driver(struct memstick_driver *drv) |
| 627 | { |
| 628 | driver_unregister(drv: &drv->driver); |
| 629 | } |
| 630 | EXPORT_SYMBOL(memstick_unregister_driver); |
| 631 | |
| 632 | |
| 633 | static int __init memstick_init(void) |
| 634 | { |
| 635 | int rc; |
| 636 | |
| 637 | workqueue = create_freezable_workqueue("kmemstick" ); |
| 638 | if (!workqueue) |
| 639 | return -ENOMEM; |
| 640 | |
| 641 | rc = bus_register(bus: &memstick_bus_type); |
| 642 | if (rc) |
| 643 | goto error_destroy_workqueue; |
| 644 | |
| 645 | rc = class_register(class: &memstick_host_class); |
| 646 | if (rc) |
| 647 | goto error_bus_unregister; |
| 648 | |
| 649 | return 0; |
| 650 | |
| 651 | error_bus_unregister: |
| 652 | bus_unregister(bus: &memstick_bus_type); |
| 653 | error_destroy_workqueue: |
| 654 | destroy_workqueue(wq: workqueue); |
| 655 | |
| 656 | return rc; |
| 657 | } |
| 658 | |
| 659 | static void __exit memstick_exit(void) |
| 660 | { |
| 661 | class_unregister(class: &memstick_host_class); |
| 662 | bus_unregister(bus: &memstick_bus_type); |
| 663 | destroy_workqueue(wq: workqueue); |
| 664 | idr_destroy(&memstick_host_idr); |
| 665 | } |
| 666 | |
| 667 | module_init(memstick_init); |
| 668 | module_exit(memstick_exit); |
| 669 | |
| 670 | MODULE_AUTHOR("Alex Dubov" ); |
| 671 | MODULE_LICENSE("GPL" ); |
| 672 | MODULE_DESCRIPTION("Sony MemoryStick core driver" ); |
| 673 | |