1 /* 2 * Sony MemoryStick Pro storage support 3 * 4 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 * 10 * Special thanks to Carlos Corbacho for providing various MemoryStick cards 11 * that made this driver possible. 12 * 13 */ 14 15 #include <linux/blk-mq.h> 16 #include <linux/idr.h> 17 #include <linux/hdreg.h> 18 #include <linux/kthread.h> 19 #include <linux/delay.h> 20 #include <linux/slab.h> 21 #include <linux/mutex.h> 22 #include <linux/memstick.h> 23 #include <linux/module.h> 24 25 #define DRIVER_NAME "mspro_block" 26 27 static int major; 28 module_param(major, int, 0644); 29 30 #define MSPRO_BLOCK_MAX_SEGS 32 31 #define MSPRO_BLOCK_MAX_PAGES ((2 << 16) - 1) 32 33 #define MSPRO_BLOCK_SIGNATURE 0xa5c3 34 #define MSPRO_BLOCK_MAX_ATTRIBUTES 41 35 36 #define MSPRO_BLOCK_PART_SHIFT 3 37 38 enum { 39 MSPRO_BLOCK_ID_SYSINFO = 0x10, 40 MSPRO_BLOCK_ID_MODELNAME = 0x15, 41 MSPRO_BLOCK_ID_MBR = 0x20, 42 MSPRO_BLOCK_ID_PBR16 = 0x21, 43 MSPRO_BLOCK_ID_PBR32 = 0x22, 44 MSPRO_BLOCK_ID_SPECFILEVALUES1 = 0x25, 45 MSPRO_BLOCK_ID_SPECFILEVALUES2 = 0x26, 46 MSPRO_BLOCK_ID_DEVINFO = 0x30 47 }; 48 49 struct mspro_sys_attr { 50 size_t size; 51 void *data; 52 unsigned char id; 53 char name[32]; 54 struct device_attribute dev_attr; 55 }; 56 57 struct mspro_attr_entry { 58 __be32 address; 59 __be32 size; 60 unsigned char id; 61 unsigned char reserved[3]; 62 } __attribute__((packed)); 63 64 struct mspro_attribute { 65 __be16 signature; 66 unsigned short version; 67 unsigned char count; 68 unsigned char reserved[11]; 69 struct mspro_attr_entry entries[]; 70 } __attribute__((packed)); 71 72 struct mspro_sys_info { 73 unsigned char class; 74 unsigned char reserved0; 75 __be16 block_size; 76 __be16 block_count; 77 __be16 user_block_count; 78 __be16 page_size; 79 unsigned char reserved1[2]; 80 unsigned char assembly_date[8]; 81 __be32 serial_number; 82 unsigned char assembly_maker_code; 83 unsigned char assembly_model_code[3]; 84 __be16 memory_maker_code; 85 __be16 memory_model_code; 86 unsigned char reserved2[4]; 87 unsigned char vcc; 88 unsigned char vpp; 89 __be16 controller_number; 90 __be16 controller_function; 91 __be16 start_sector; 92 __be16 unit_size; 93 unsigned char ms_sub_class; 94 unsigned char reserved3[4]; 95 unsigned char interface_type; 96 __be16 controller_code; 97 unsigned char format_type; 98 unsigned char reserved4; 99 unsigned char device_type; 100 unsigned char reserved5[7]; 101 unsigned char mspro_id[16]; 102 unsigned char reserved6[16]; 103 } __attribute__((packed)); 104 105 struct mspro_mbr { 106 unsigned char boot_partition; 107 unsigned char start_head; 108 unsigned char start_sector; 109 unsigned char start_cylinder; 110 unsigned char partition_type; 111 unsigned char end_head; 112 unsigned char end_sector; 113 unsigned char end_cylinder; 114 unsigned int start_sectors; 115 unsigned int sectors_per_partition; 116 } __attribute__((packed)); 117 118 struct mspro_specfile { 119 char name[8]; 120 char ext[3]; 121 unsigned char attr; 122 unsigned char reserved[10]; 123 unsigned short time; 124 unsigned short date; 125 unsigned short cluster; 126 unsigned int size; 127 } __attribute__((packed)); 128 129 struct mspro_devinfo { 130 __be16 cylinders; 131 __be16 heads; 132 __be16 bytes_per_track; 133 __be16 bytes_per_sector; 134 __be16 sectors_per_track; 135 unsigned char reserved[6]; 136 } __attribute__((packed)); 137 138 struct mspro_block_data { 139 struct memstick_dev *card; 140 unsigned int usage_count; 141 unsigned int caps; 142 struct gendisk *disk; 143 struct request_queue *queue; 144 struct request *block_req; 145 struct blk_mq_tag_set tag_set; 146 spinlock_t q_lock; 147 148 unsigned short page_size; 149 unsigned short cylinders; 150 unsigned short heads; 151 unsigned short sectors_per_track; 152 153 unsigned char system; 154 unsigned char read_only:1, 155 eject:1, 156 data_dir:1, 157 active:1; 158 unsigned char transfer_cmd; 159 160 int (*mrq_handler)(struct memstick_dev *card, 161 struct memstick_request **mrq); 162 163 164 /* Default request setup function for data access method preferred by 165 * this host instance. 166 */ 167 void (*setup_transfer)(struct memstick_dev *card, 168 u64 offset, size_t length); 169 170 struct attribute_group attr_group; 171 172 struct scatterlist req_sg[MSPRO_BLOCK_MAX_SEGS]; 173 unsigned int seg_count; 174 unsigned int current_seg; 175 unsigned int current_page; 176 }; 177 178 static DEFINE_IDR(mspro_block_disk_idr); 179 static DEFINE_MUTEX(mspro_block_disk_lock); 180 181 static int mspro_block_complete_req(struct memstick_dev *card, int error); 182 183 /*** Block device ***/ 184 185 static int mspro_block_bd_open(struct block_device *bdev, fmode_t mode) 186 { 187 struct gendisk *disk = bdev->bd_disk; 188 struct mspro_block_data *msb = disk->private_data; 189 int rc = -ENXIO; 190 191 mutex_lock(&mspro_block_disk_lock); 192 193 if (msb && msb->card) { 194 msb->usage_count++; 195 if ((mode & FMODE_WRITE) && msb->read_only) 196 rc = -EROFS; 197 else 198 rc = 0; 199 } 200 201 mutex_unlock(&mspro_block_disk_lock); 202 203 return rc; 204 } 205 206 207 static void mspro_block_disk_release(struct gendisk *disk) 208 { 209 struct mspro_block_data *msb = disk->private_data; 210 int disk_id = MINOR(disk_devt(disk)) >> MSPRO_BLOCK_PART_SHIFT; 211 212 mutex_lock(&mspro_block_disk_lock); 213 214 if (msb) { 215 if (msb->usage_count) 216 msb->usage_count--; 217 218 if (!msb->usage_count) { 219 kfree(msb); 220 disk->private_data = NULL; 221 idr_remove(&mspro_block_disk_idr, disk_id); 222 put_disk(disk); 223 } 224 } 225 226 mutex_unlock(&mspro_block_disk_lock); 227 } 228 229 static void mspro_block_bd_release(struct gendisk *disk, fmode_t mode) 230 { 231 mspro_block_disk_release(disk); 232 } 233 234 static int mspro_block_bd_getgeo(struct block_device *bdev, 235 struct hd_geometry *geo) 236 { 237 struct mspro_block_data *msb = bdev->bd_disk->private_data; 238 239 geo->heads = msb->heads; 240 geo->sectors = msb->sectors_per_track; 241 geo->cylinders = msb->cylinders; 242 243 return 0; 244 } 245 246 static const struct block_device_operations ms_block_bdops = { 247 .open = mspro_block_bd_open, 248 .release = mspro_block_bd_release, 249 .getgeo = mspro_block_bd_getgeo, 250 .owner = THIS_MODULE 251 }; 252 253 /*** Information ***/ 254 255 static struct mspro_sys_attr *mspro_from_sysfs_attr(struct attribute *attr) 256 { 257 struct device_attribute *dev_attr 258 = container_of(attr, struct device_attribute, attr); 259 return container_of(dev_attr, struct mspro_sys_attr, dev_attr); 260 } 261 262 static const char *mspro_block_attr_name(unsigned char tag) 263 { 264 switch (tag) { 265 case MSPRO_BLOCK_ID_SYSINFO: 266 return "attr_sysinfo"; 267 case MSPRO_BLOCK_ID_MODELNAME: 268 return "attr_modelname"; 269 case MSPRO_BLOCK_ID_MBR: 270 return "attr_mbr"; 271 case MSPRO_BLOCK_ID_PBR16: 272 return "attr_pbr16"; 273 case MSPRO_BLOCK_ID_PBR32: 274 return "attr_pbr32"; 275 case MSPRO_BLOCK_ID_SPECFILEVALUES1: 276 return "attr_specfilevalues1"; 277 case MSPRO_BLOCK_ID_SPECFILEVALUES2: 278 return "attr_specfilevalues2"; 279 case MSPRO_BLOCK_ID_DEVINFO: 280 return "attr_devinfo"; 281 default: 282 return NULL; 283 }; 284 } 285 286 typedef ssize_t (*sysfs_show_t)(struct device *dev, 287 struct device_attribute *attr, 288 char *buffer); 289 290 static ssize_t mspro_block_attr_show_default(struct device *dev, 291 struct device_attribute *attr, 292 char *buffer) 293 { 294 struct mspro_sys_attr *s_attr = container_of(attr, 295 struct mspro_sys_attr, 296 dev_attr); 297 298 ssize_t cnt, rc = 0; 299 300 for (cnt = 0; cnt < s_attr->size; cnt++) { 301 if (cnt && !(cnt % 16)) { 302 if (PAGE_SIZE - rc) 303 buffer[rc++] = '\n'; 304 } 305 306 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ", 307 ((unsigned char *)s_attr->data)[cnt]); 308 } 309 return rc; 310 } 311 312 static ssize_t mspro_block_attr_show_sysinfo(struct device *dev, 313 struct device_attribute *attr, 314 char *buffer) 315 { 316 struct mspro_sys_attr *x_attr = container_of(attr, 317 struct mspro_sys_attr, 318 dev_attr); 319 struct mspro_sys_info *x_sys = x_attr->data; 320 ssize_t rc = 0; 321 int date_tz = 0, date_tz_f = 0; 322 323 if (x_sys->assembly_date[0] > 0x80U) { 324 date_tz = (~x_sys->assembly_date[0]) + 1; 325 date_tz_f = date_tz & 3; 326 date_tz >>= 2; 327 date_tz = -date_tz; 328 date_tz_f *= 15; 329 } else if (x_sys->assembly_date[0] < 0x80U) { 330 date_tz = x_sys->assembly_date[0]; 331 date_tz_f = date_tz & 3; 332 date_tz >>= 2; 333 date_tz_f *= 15; 334 } 335 336 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "class: %x\n", 337 x_sys->class); 338 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block size: %x\n", 339 be16_to_cpu(x_sys->block_size)); 340 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block count: %x\n", 341 be16_to_cpu(x_sys->block_count)); 342 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "user block count: %x\n", 343 be16_to_cpu(x_sys->user_block_count)); 344 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "page size: %x\n", 345 be16_to_cpu(x_sys->page_size)); 346 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly date: " 347 "GMT%+d:%d %04u-%02u-%02u %02u:%02u:%02u\n", 348 date_tz, date_tz_f, 349 be16_to_cpup((__be16 *)&x_sys->assembly_date[1]), 350 x_sys->assembly_date[3], x_sys->assembly_date[4], 351 x_sys->assembly_date[5], x_sys->assembly_date[6], 352 x_sys->assembly_date[7]); 353 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "serial number: %x\n", 354 be32_to_cpu(x_sys->serial_number)); 355 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, 356 "assembly maker code: %x\n", 357 x_sys->assembly_maker_code); 358 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly model code: " 359 "%02x%02x%02x\n", x_sys->assembly_model_code[0], 360 x_sys->assembly_model_code[1], 361 x_sys->assembly_model_code[2]); 362 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory maker code: %x\n", 363 be16_to_cpu(x_sys->memory_maker_code)); 364 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory model code: %x\n", 365 be16_to_cpu(x_sys->memory_model_code)); 366 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vcc: %x\n", 367 x_sys->vcc); 368 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vpp: %x\n", 369 x_sys->vpp); 370 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller number: %x\n", 371 be16_to_cpu(x_sys->controller_number)); 372 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, 373 "controller function: %x\n", 374 be16_to_cpu(x_sys->controller_function)); 375 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n", 376 be16_to_cpu(x_sys->start_sector)); 377 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "unit size: %x\n", 378 be16_to_cpu(x_sys->unit_size)); 379 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sub class: %x\n", 380 x_sys->ms_sub_class); 381 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "interface type: %x\n", 382 x_sys->interface_type); 383 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller code: %x\n", 384 be16_to_cpu(x_sys->controller_code)); 385 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "format type: %x\n", 386 x_sys->format_type); 387 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "device type: %x\n", 388 x_sys->device_type); 389 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "mspro id: %s\n", 390 x_sys->mspro_id); 391 return rc; 392 } 393 394 static ssize_t mspro_block_attr_show_modelname(struct device *dev, 395 struct device_attribute *attr, 396 char *buffer) 397 { 398 struct mspro_sys_attr *s_attr = container_of(attr, 399 struct mspro_sys_attr, 400 dev_attr); 401 402 return scnprintf(buffer, PAGE_SIZE, "%s", (char *)s_attr->data); 403 } 404 405 static ssize_t mspro_block_attr_show_mbr(struct device *dev, 406 struct device_attribute *attr, 407 char *buffer) 408 { 409 struct mspro_sys_attr *x_attr = container_of(attr, 410 struct mspro_sys_attr, 411 dev_attr); 412 struct mspro_mbr *x_mbr = x_attr->data; 413 ssize_t rc = 0; 414 415 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "boot partition: %x\n", 416 x_mbr->boot_partition); 417 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start head: %x\n", 418 x_mbr->start_head); 419 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n", 420 x_mbr->start_sector); 421 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cylinder: %x\n", 422 x_mbr->start_cylinder); 423 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "partition type: %x\n", 424 x_mbr->partition_type); 425 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end head: %x\n", 426 x_mbr->end_head); 427 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end sector: %x\n", 428 x_mbr->end_sector); 429 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end cylinder: %x\n", 430 x_mbr->end_cylinder); 431 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sectors: %x\n", 432 x_mbr->start_sectors); 433 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, 434 "sectors per partition: %x\n", 435 x_mbr->sectors_per_partition); 436 return rc; 437 } 438 439 static ssize_t mspro_block_attr_show_specfile(struct device *dev, 440 struct device_attribute *attr, 441 char *buffer) 442 { 443 struct mspro_sys_attr *x_attr = container_of(attr, 444 struct mspro_sys_attr, 445 dev_attr); 446 struct mspro_specfile *x_spfile = x_attr->data; 447 char name[9], ext[4]; 448 ssize_t rc = 0; 449 450 memcpy(name, x_spfile->name, 8); 451 name[8] = 0; 452 memcpy(ext, x_spfile->ext, 3); 453 ext[3] = 0; 454 455 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "name: %s\n", name); 456 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "ext: %s\n", ext); 457 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "attribute: %x\n", 458 x_spfile->attr); 459 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "time: %d:%d:%d\n", 460 x_spfile->time >> 11, 461 (x_spfile->time >> 5) & 0x3f, 462 (x_spfile->time & 0x1f) * 2); 463 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "date: %d-%d-%d\n", 464 (x_spfile->date >> 9) + 1980, 465 (x_spfile->date >> 5) & 0xf, 466 x_spfile->date & 0x1f); 467 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cluster: %x\n", 468 x_spfile->cluster); 469 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "size: %x\n", 470 x_spfile->size); 471 return rc; 472 } 473 474 static ssize_t mspro_block_attr_show_devinfo(struct device *dev, 475 struct device_attribute *attr, 476 char *buffer) 477 { 478 struct mspro_sys_attr *x_attr = container_of(attr, 479 struct mspro_sys_attr, 480 dev_attr); 481 struct mspro_devinfo *x_devinfo = x_attr->data; 482 ssize_t rc = 0; 483 484 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "cylinders: %x\n", 485 be16_to_cpu(x_devinfo->cylinders)); 486 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "heads: %x\n", 487 be16_to_cpu(x_devinfo->heads)); 488 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per track: %x\n", 489 be16_to_cpu(x_devinfo->bytes_per_track)); 490 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per sector: %x\n", 491 be16_to_cpu(x_devinfo->bytes_per_sector)); 492 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sectors per track: %x\n", 493 be16_to_cpu(x_devinfo->sectors_per_track)); 494 return rc; 495 } 496 497 static sysfs_show_t mspro_block_attr_show(unsigned char tag) 498 { 499 switch (tag) { 500 case MSPRO_BLOCK_ID_SYSINFO: 501 return mspro_block_attr_show_sysinfo; 502 case MSPRO_BLOCK_ID_MODELNAME: 503 return mspro_block_attr_show_modelname; 504 case MSPRO_BLOCK_ID_MBR: 505 return mspro_block_attr_show_mbr; 506 case MSPRO_BLOCK_ID_SPECFILEVALUES1: 507 case MSPRO_BLOCK_ID_SPECFILEVALUES2: 508 return mspro_block_attr_show_specfile; 509 case MSPRO_BLOCK_ID_DEVINFO: 510 return mspro_block_attr_show_devinfo; 511 default: 512 return mspro_block_attr_show_default; 513 } 514 } 515 516 /*** Protocol handlers ***/ 517 518 /* 519 * Functions prefixed with "h_" are protocol callbacks. They can be called from 520 * interrupt context. Return value of 0 means that request processing is still 521 * ongoing, while special error value of -EAGAIN means that current request is 522 * finished (and request processor should come back some time later). 523 */ 524 525 static int h_mspro_block_req_init(struct memstick_dev *card, 526 struct memstick_request **mrq) 527 { 528 struct mspro_block_data *msb = memstick_get_drvdata(card); 529 530 *mrq = &card->current_mrq; 531 card->next_request = msb->mrq_handler; 532 return 0; 533 } 534 535 static int h_mspro_block_default(struct memstick_dev *card, 536 struct memstick_request **mrq) 537 { 538 return mspro_block_complete_req(card, (*mrq)->error); 539 } 540 541 static int h_mspro_block_default_bad(struct memstick_dev *card, 542 struct memstick_request **mrq) 543 { 544 return -ENXIO; 545 } 546 547 static int h_mspro_block_get_ro(struct memstick_dev *card, 548 struct memstick_request **mrq) 549 { 550 struct mspro_block_data *msb = memstick_get_drvdata(card); 551 552 if (!(*mrq)->error) { 553 if ((*mrq)->data[offsetof(struct ms_status_register, status0)] 554 & MEMSTICK_STATUS0_WP) 555 msb->read_only = 1; 556 else 557 msb->read_only = 0; 558 } 559 560 return mspro_block_complete_req(card, (*mrq)->error); 561 } 562 563 static int h_mspro_block_wait_for_ced(struct memstick_dev *card, 564 struct memstick_request **mrq) 565 { 566 dev_dbg(&card->dev, "wait for ced: value %x\n", (*mrq)->data[0]); 567 568 if (!(*mrq)->error) { 569 if ((*mrq)->data[0] & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)) 570 (*mrq)->error = -EFAULT; 571 else if (!((*mrq)->data[0] & MEMSTICK_INT_CED)) 572 return 0; 573 } 574 575 return mspro_block_complete_req(card, (*mrq)->error); 576 } 577 578 static int h_mspro_block_transfer_data(struct memstick_dev *card, 579 struct memstick_request **mrq) 580 { 581 struct mspro_block_data *msb = memstick_get_drvdata(card); 582 unsigned char t_val = 0; 583 struct scatterlist t_sg = { 0 }; 584 size_t t_offset; 585 586 if ((*mrq)->error) 587 return mspro_block_complete_req(card, (*mrq)->error); 588 589 switch ((*mrq)->tpc) { 590 case MS_TPC_WRITE_REG: 591 memstick_init_req(*mrq, MS_TPC_SET_CMD, &msb->transfer_cmd, 1); 592 (*mrq)->need_card_int = 1; 593 return 0; 594 case MS_TPC_SET_CMD: 595 t_val = (*mrq)->int_reg; 596 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1); 597 if (msb->caps & MEMSTICK_CAP_AUTO_GET_INT) 598 goto has_int_reg; 599 return 0; 600 case MS_TPC_GET_INT: 601 t_val = (*mrq)->data[0]; 602 has_int_reg: 603 if (t_val & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)) { 604 t_val = MSPRO_CMD_STOP; 605 memstick_init_req(*mrq, MS_TPC_SET_CMD, &t_val, 1); 606 card->next_request = h_mspro_block_default; 607 return 0; 608 } 609 610 if (msb->current_page 611 == (msb->req_sg[msb->current_seg].length 612 / msb->page_size)) { 613 msb->current_page = 0; 614 msb->current_seg++; 615 616 if (msb->current_seg == msb->seg_count) { 617 if (t_val & MEMSTICK_INT_CED) { 618 return mspro_block_complete_req(card, 619 0); 620 } else { 621 card->next_request 622 = h_mspro_block_wait_for_ced; 623 memstick_init_req(*mrq, MS_TPC_GET_INT, 624 NULL, 1); 625 return 0; 626 } 627 } 628 } 629 630 if (!(t_val & MEMSTICK_INT_BREQ)) { 631 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1); 632 return 0; 633 } 634 635 t_offset = msb->req_sg[msb->current_seg].offset; 636 t_offset += msb->current_page * msb->page_size; 637 638 sg_set_page(&t_sg, 639 nth_page(sg_page(&(msb->req_sg[msb->current_seg])), 640 t_offset >> PAGE_SHIFT), 641 msb->page_size, offset_in_page(t_offset)); 642 643 memstick_init_req_sg(*mrq, msb->data_dir == READ 644 ? MS_TPC_READ_LONG_DATA 645 : MS_TPC_WRITE_LONG_DATA, 646 &t_sg); 647 (*mrq)->need_card_int = 1; 648 return 0; 649 case MS_TPC_READ_LONG_DATA: 650 case MS_TPC_WRITE_LONG_DATA: 651 msb->current_page++; 652 if (msb->caps & MEMSTICK_CAP_AUTO_GET_INT) { 653 t_val = (*mrq)->int_reg; 654 goto has_int_reg; 655 } else { 656 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1); 657 return 0; 658 } 659 660 default: 661 BUG(); 662 } 663 } 664 665 /*** Transfer setup functions for different access methods. ***/ 666 667 /** Setup data transfer request for SET_CMD TPC with arguments in card 668 * registers. 669 * 670 * @card Current media instance 671 * @offset Target data offset in bytes 672 * @length Required transfer length in bytes. 673 */ 674 static void h_mspro_block_setup_cmd(struct memstick_dev *card, u64 offset, 675 size_t length) 676 { 677 struct mspro_block_data *msb = memstick_get_drvdata(card); 678 struct mspro_param_register param = { 679 .system = msb->system, 680 .data_count = cpu_to_be16((uint16_t)(length / msb->page_size)), 681 /* ISO C90 warning precludes direct initialization for now. */ 682 .data_address = 0, 683 .tpc_param = 0 684 }; 685 686 do_div(offset, msb->page_size); 687 param.data_address = cpu_to_be32((uint32_t)offset); 688 689 card->next_request = h_mspro_block_req_init; 690 msb->mrq_handler = h_mspro_block_transfer_data; 691 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, 692 ¶m, sizeof(param)); 693 } 694 695 /*** Data transfer ***/ 696 697 static int mspro_block_issue_req(struct memstick_dev *card) 698 { 699 struct mspro_block_data *msb = memstick_get_drvdata(card); 700 u64 t_off; 701 unsigned int count; 702 703 while (true) { 704 msb->current_page = 0; 705 msb->current_seg = 0; 706 msb->seg_count = blk_rq_map_sg(msb->block_req->q, 707 msb->block_req, 708 msb->req_sg); 709 710 if (!msb->seg_count) { 711 unsigned int bytes = blk_rq_cur_bytes(msb->block_req); 712 bool chunk; 713 714 chunk = blk_update_request(msb->block_req, 715 BLK_STS_RESOURCE, 716 bytes); 717 if (chunk) 718 continue; 719 __blk_mq_end_request(msb->block_req, 720 BLK_STS_RESOURCE); 721 msb->block_req = NULL; 722 return -EAGAIN; 723 } 724 725 t_off = blk_rq_pos(msb->block_req); 726 t_off <<= 9; 727 count = blk_rq_bytes(msb->block_req); 728 729 msb->setup_transfer(card, t_off, count); 730 731 msb->data_dir = rq_data_dir(msb->block_req); 732 msb->transfer_cmd = msb->data_dir == READ 733 ? MSPRO_CMD_READ_DATA 734 : MSPRO_CMD_WRITE_DATA; 735 736 memstick_new_req(card->host); 737 return 0; 738 } 739 } 740 741 static int mspro_block_complete_req(struct memstick_dev *card, int error) 742 { 743 struct mspro_block_data *msb = memstick_get_drvdata(card); 744 int cnt; 745 bool chunk; 746 unsigned int t_len = 0; 747 unsigned long flags; 748 749 spin_lock_irqsave(&msb->q_lock, flags); 750 dev_dbg(&card->dev, "complete %d, %d\n", msb->block_req ? 1 : 0, 751 error); 752 753 if (msb->block_req) { 754 /* Nothing to do - not really an error */ 755 if (error == -EAGAIN) 756 error = 0; 757 758 if (error || (card->current_mrq.tpc == MSPRO_CMD_STOP)) { 759 if (msb->data_dir == READ) { 760 for (cnt = 0; cnt < msb->current_seg; cnt++) { 761 t_len += msb->req_sg[cnt].length 762 / msb->page_size; 763 764 if (msb->current_page) 765 t_len += msb->current_page - 1; 766 767 t_len *= msb->page_size; 768 } 769 } 770 } else 771 t_len = blk_rq_bytes(msb->block_req); 772 773 dev_dbg(&card->dev, "transferred %x (%d)\n", t_len, error); 774 775 if (error && !t_len) 776 t_len = blk_rq_cur_bytes(msb->block_req); 777 778 chunk = blk_update_request(msb->block_req, 779 errno_to_blk_status(error), t_len); 780 if (chunk) { 781 error = mspro_block_issue_req(card); 782 if (!error) 783 goto out; 784 } else { 785 __blk_mq_end_request(msb->block_req, 786 errno_to_blk_status(error)); 787 msb->block_req = NULL; 788 } 789 } else { 790 if (!error) 791 error = -EAGAIN; 792 } 793 794 card->next_request = h_mspro_block_default_bad; 795 complete_all(&card->mrq_complete); 796 out: 797 spin_unlock_irqrestore(&msb->q_lock, flags); 798 return error; 799 } 800 801 static void mspro_block_stop(struct memstick_dev *card) 802 { 803 struct mspro_block_data *msb = memstick_get_drvdata(card); 804 int rc = 0; 805 unsigned long flags; 806 807 while (1) { 808 spin_lock_irqsave(&msb->q_lock, flags); 809 if (!msb->block_req) { 810 blk_mq_stop_hw_queues(msb->queue); 811 rc = 1; 812 } 813 spin_unlock_irqrestore(&msb->q_lock, flags); 814 815 if (rc) 816 break; 817 818 wait_for_completion(&card->mrq_complete); 819 } 820 } 821 822 static void mspro_block_start(struct memstick_dev *card) 823 { 824 struct mspro_block_data *msb = memstick_get_drvdata(card); 825 826 blk_mq_start_hw_queues(msb->queue); 827 } 828 829 static blk_status_t mspro_queue_rq(struct blk_mq_hw_ctx *hctx, 830 const struct blk_mq_queue_data *bd) 831 { 832 struct memstick_dev *card = hctx->queue->queuedata; 833 struct mspro_block_data *msb = memstick_get_drvdata(card); 834 835 spin_lock_irq(&msb->q_lock); 836 837 if (msb->block_req) { 838 spin_unlock_irq(&msb->q_lock); 839 return BLK_STS_DEV_RESOURCE; 840 } 841 842 if (msb->eject) { 843 spin_unlock_irq(&msb->q_lock); 844 blk_mq_start_request(bd->rq); 845 return BLK_STS_IOERR; 846 } 847 848 msb->block_req = bd->rq; 849 blk_mq_start_request(bd->rq); 850 851 if (mspro_block_issue_req(card)) 852 msb->block_req = NULL; 853 854 spin_unlock_irq(&msb->q_lock); 855 return BLK_STS_OK; 856 } 857 858 /*** Initialization ***/ 859 860 static int mspro_block_wait_for_ced(struct memstick_dev *card) 861 { 862 struct mspro_block_data *msb = memstick_get_drvdata(card); 863 864 card->next_request = h_mspro_block_req_init; 865 msb->mrq_handler = h_mspro_block_wait_for_ced; 866 memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1); 867 memstick_new_req(card->host); 868 wait_for_completion(&card->mrq_complete); 869 return card->current_mrq.error; 870 } 871 872 static int mspro_block_set_interface(struct memstick_dev *card, 873 unsigned char sys_reg) 874 { 875 struct memstick_host *host = card->host; 876 struct mspro_block_data *msb = memstick_get_drvdata(card); 877 struct mspro_param_register param = { 878 .system = sys_reg, 879 .data_count = 0, 880 .data_address = 0, 881 .tpc_param = 0 882 }; 883 884 card->next_request = h_mspro_block_req_init; 885 msb->mrq_handler = h_mspro_block_default; 886 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, ¶m, 887 sizeof(param)); 888 memstick_new_req(host); 889 wait_for_completion(&card->mrq_complete); 890 return card->current_mrq.error; 891 } 892 893 static int mspro_block_switch_interface(struct memstick_dev *card) 894 { 895 struct memstick_host *host = card->host; 896 struct mspro_block_data *msb = memstick_get_drvdata(card); 897 int rc = 0; 898 899 try_again: 900 if (msb->caps & MEMSTICK_CAP_PAR4) 901 rc = mspro_block_set_interface(card, MEMSTICK_SYS_PAR4); 902 else 903 return 0; 904 905 if (rc) { 906 printk(KERN_WARNING 907 "%s: could not switch to 4-bit mode, error %d\n", 908 dev_name(&card->dev), rc); 909 return 0; 910 } 911 912 msb->system = MEMSTICK_SYS_PAR4; 913 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PAR4); 914 printk(KERN_INFO "%s: switching to 4-bit parallel mode\n", 915 dev_name(&card->dev)); 916 917 if (msb->caps & MEMSTICK_CAP_PAR8) { 918 rc = mspro_block_set_interface(card, MEMSTICK_SYS_PAR8); 919 920 if (!rc) { 921 msb->system = MEMSTICK_SYS_PAR8; 922 host->set_param(host, MEMSTICK_INTERFACE, 923 MEMSTICK_PAR8); 924 printk(KERN_INFO 925 "%s: switching to 8-bit parallel mode\n", 926 dev_name(&card->dev)); 927 } else 928 printk(KERN_WARNING 929 "%s: could not switch to 8-bit mode, error %d\n", 930 dev_name(&card->dev), rc); 931 } 932 933 card->next_request = h_mspro_block_req_init; 934 msb->mrq_handler = h_mspro_block_default; 935 memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1); 936 memstick_new_req(card->host); 937 wait_for_completion(&card->mrq_complete); 938 rc = card->current_mrq.error; 939 940 if (rc) { 941 printk(KERN_WARNING 942 "%s: interface error, trying to fall back to serial\n", 943 dev_name(&card->dev)); 944 msb->system = MEMSTICK_SYS_SERIAL; 945 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF); 946 msleep(10); 947 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON); 948 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL); 949 950 rc = memstick_set_rw_addr(card); 951 if (!rc) 952 rc = mspro_block_set_interface(card, msb->system); 953 954 if (!rc) { 955 msleep(150); 956 rc = mspro_block_wait_for_ced(card); 957 if (rc) 958 return rc; 959 960 if (msb->caps & MEMSTICK_CAP_PAR8) { 961 msb->caps &= ~MEMSTICK_CAP_PAR8; 962 goto try_again; 963 } 964 } 965 } 966 return rc; 967 } 968 969 /* Memory allocated for attributes by this function should be freed by 970 * mspro_block_data_clear, no matter if the initialization process succeeded 971 * or failed. 972 */ 973 static int mspro_block_read_attributes(struct memstick_dev *card) 974 { 975 struct mspro_block_data *msb = memstick_get_drvdata(card); 976 struct mspro_attribute *attr = NULL; 977 struct mspro_sys_attr *s_attr = NULL; 978 unsigned char *buffer = NULL; 979 int cnt, rc, attr_count; 980 /* While normally physical device offsets, represented here by 981 * attr_offset and attr_len will be of large numeric types, we can be 982 * sure, that attributes are close enough to the beginning of the 983 * device, to save ourselves some trouble. 984 */ 985 unsigned int addr, attr_offset = 0, attr_len = msb->page_size; 986 987 attr = kmalloc(msb->page_size, GFP_KERNEL); 988 if (!attr) 989 return -ENOMEM; 990 991 sg_init_one(&msb->req_sg[0], attr, msb->page_size); 992 msb->seg_count = 1; 993 msb->current_seg = 0; 994 msb->current_page = 0; 995 msb->data_dir = READ; 996 msb->transfer_cmd = MSPRO_CMD_READ_ATRB; 997 998 msb->setup_transfer(card, attr_offset, attr_len); 999 1000 memstick_new_req(card->host); 1001 wait_for_completion(&card->mrq_complete); 1002 if (card->current_mrq.error) { 1003 rc = card->current_mrq.error; 1004 goto out_free_attr; 1005 } 1006 1007 if (be16_to_cpu(attr->signature) != MSPRO_BLOCK_SIGNATURE) { 1008 printk(KERN_ERR "%s: unrecognized device signature %x\n", 1009 dev_name(&card->dev), be16_to_cpu(attr->signature)); 1010 rc = -ENODEV; 1011 goto out_free_attr; 1012 } 1013 1014 if (attr->count > MSPRO_BLOCK_MAX_ATTRIBUTES) { 1015 printk(KERN_WARNING "%s: way too many attribute entries\n", 1016 dev_name(&card->dev)); 1017 attr_count = MSPRO_BLOCK_MAX_ATTRIBUTES; 1018 } else 1019 attr_count = attr->count; 1020 1021 msb->attr_group.attrs = kcalloc(attr_count + 1, 1022 sizeof(*msb->attr_group.attrs), 1023 GFP_KERNEL); 1024 if (!msb->attr_group.attrs) { 1025 rc = -ENOMEM; 1026 goto out_free_attr; 1027 } 1028 msb->attr_group.name = "media_attributes"; 1029 1030 buffer = kmemdup(attr, attr_len, GFP_KERNEL); 1031 if (!buffer) { 1032 rc = -ENOMEM; 1033 goto out_free_attr; 1034 } 1035 1036 for (cnt = 0; cnt < attr_count; ++cnt) { 1037 s_attr = kzalloc(sizeof(struct mspro_sys_attr), GFP_KERNEL); 1038 if (!s_attr) { 1039 rc = -ENOMEM; 1040 goto out_free_buffer; 1041 } 1042 1043 msb->attr_group.attrs[cnt] = &s_attr->dev_attr.attr; 1044 addr = be32_to_cpu(attr->entries[cnt].address); 1045 s_attr->size = be32_to_cpu(attr->entries[cnt].size); 1046 dev_dbg(&card->dev, "adding attribute %d: id %x, address %x, " 1047 "size %zx\n", cnt, attr->entries[cnt].id, addr, 1048 s_attr->size); 1049 s_attr->id = attr->entries[cnt].id; 1050 if (mspro_block_attr_name(s_attr->id)) 1051 snprintf(s_attr->name, sizeof(s_attr->name), "%s", 1052 mspro_block_attr_name(attr->entries[cnt].id)); 1053 else 1054 snprintf(s_attr->name, sizeof(s_attr->name), 1055 "attr_x%02x", attr->entries[cnt].id); 1056 1057 sysfs_attr_init(&s_attr->dev_attr.attr); 1058 s_attr->dev_attr.attr.name = s_attr->name; 1059 s_attr->dev_attr.attr.mode = S_IRUGO; 1060 s_attr->dev_attr.show = mspro_block_attr_show(s_attr->id); 1061 1062 if (!s_attr->size) 1063 continue; 1064 1065 s_attr->data = kmalloc(s_attr->size, GFP_KERNEL); 1066 if (!s_attr->data) { 1067 rc = -ENOMEM; 1068 goto out_free_buffer; 1069 } 1070 1071 if (((addr / msb->page_size) == (attr_offset / msb->page_size)) 1072 && (((addr + s_attr->size - 1) / msb->page_size) 1073 == (attr_offset / msb->page_size))) { 1074 memcpy(s_attr->data, buffer + addr % msb->page_size, 1075 s_attr->size); 1076 continue; 1077 } 1078 1079 attr_offset = (addr / msb->page_size) * msb->page_size; 1080 1081 if ((attr_offset + attr_len) < (addr + s_attr->size)) { 1082 kfree(buffer); 1083 attr_len = (((addr + s_attr->size) / msb->page_size) 1084 + 1 ) * msb->page_size - attr_offset; 1085 buffer = kmalloc(attr_len, GFP_KERNEL); 1086 if (!buffer) { 1087 rc = -ENOMEM; 1088 goto out_free_attr; 1089 } 1090 } 1091 1092 sg_init_one(&msb->req_sg[0], buffer, attr_len); 1093 msb->seg_count = 1; 1094 msb->current_seg = 0; 1095 msb->current_page = 0; 1096 msb->data_dir = READ; 1097 msb->transfer_cmd = MSPRO_CMD_READ_ATRB; 1098 1099 dev_dbg(&card->dev, "reading attribute range %x, %x\n", 1100 attr_offset, attr_len); 1101 1102 msb->setup_transfer(card, attr_offset, attr_len); 1103 memstick_new_req(card->host); 1104 wait_for_completion(&card->mrq_complete); 1105 if (card->current_mrq.error) { 1106 rc = card->current_mrq.error; 1107 goto out_free_buffer; 1108 } 1109 1110 memcpy(s_attr->data, buffer + addr % msb->page_size, 1111 s_attr->size); 1112 } 1113 1114 rc = 0; 1115 out_free_buffer: 1116 kfree(buffer); 1117 out_free_attr: 1118 kfree(attr); 1119 return rc; 1120 } 1121 1122 static int mspro_block_init_card(struct memstick_dev *card) 1123 { 1124 struct mspro_block_data *msb = memstick_get_drvdata(card); 1125 struct memstick_host *host = card->host; 1126 int rc = 0; 1127 1128 msb->system = MEMSTICK_SYS_SERIAL; 1129 msb->setup_transfer = h_mspro_block_setup_cmd; 1130 1131 card->reg_addr.r_offset = offsetof(struct mspro_register, status); 1132 card->reg_addr.r_length = sizeof(struct ms_status_register); 1133 card->reg_addr.w_offset = offsetof(struct mspro_register, param); 1134 card->reg_addr.w_length = sizeof(struct mspro_param_register); 1135 1136 if (memstick_set_rw_addr(card)) 1137 return -EIO; 1138 1139 msb->caps = host->caps; 1140 1141 msleep(150); 1142 rc = mspro_block_wait_for_ced(card); 1143 if (rc) 1144 return rc; 1145 1146 rc = mspro_block_switch_interface(card); 1147 if (rc) 1148 return rc; 1149 1150 dev_dbg(&card->dev, "card activated\n"); 1151 if (msb->system != MEMSTICK_SYS_SERIAL) 1152 msb->caps |= MEMSTICK_CAP_AUTO_GET_INT; 1153 1154 card->next_request = h_mspro_block_req_init; 1155 msb->mrq_handler = h_mspro_block_get_ro; 1156 memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL, 1157 sizeof(struct ms_status_register)); 1158 memstick_new_req(card->host); 1159 wait_for_completion(&card->mrq_complete); 1160 if (card->current_mrq.error) 1161 return card->current_mrq.error; 1162 1163 dev_dbg(&card->dev, "card r/w status %d\n", msb->read_only ? 0 : 1); 1164 1165 msb->page_size = 512; 1166 rc = mspro_block_read_attributes(card); 1167 if (rc) 1168 return rc; 1169 1170 dev_dbg(&card->dev, "attributes loaded\n"); 1171 return 0; 1172 1173 } 1174 1175 static const struct blk_mq_ops mspro_mq_ops = { 1176 .queue_rq = mspro_queue_rq, 1177 }; 1178 1179 static int mspro_block_init_disk(struct memstick_dev *card) 1180 { 1181 struct mspro_block_data *msb = memstick_get_drvdata(card); 1182 struct mspro_devinfo *dev_info = NULL; 1183 struct mspro_sys_info *sys_info = NULL; 1184 struct mspro_sys_attr *s_attr = NULL; 1185 int rc, disk_id; 1186 unsigned long capacity; 1187 1188 for (rc = 0; msb->attr_group.attrs[rc]; ++rc) { 1189 s_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[rc]); 1190 1191 if (s_attr->id == MSPRO_BLOCK_ID_DEVINFO) 1192 dev_info = s_attr->data; 1193 else if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO) 1194 sys_info = s_attr->data; 1195 } 1196 1197 if (!dev_info || !sys_info) 1198 return -ENODEV; 1199 1200 msb->cylinders = be16_to_cpu(dev_info->cylinders); 1201 msb->heads = be16_to_cpu(dev_info->heads); 1202 msb->sectors_per_track = be16_to_cpu(dev_info->sectors_per_track); 1203 1204 msb->page_size = be16_to_cpu(sys_info->unit_size); 1205 1206 mutex_lock(&mspro_block_disk_lock); 1207 disk_id = idr_alloc(&mspro_block_disk_idr, card, 0, 256, GFP_KERNEL); 1208 mutex_unlock(&mspro_block_disk_lock); 1209 if (disk_id < 0) 1210 return disk_id; 1211 1212 msb->disk = alloc_disk(1 << MSPRO_BLOCK_PART_SHIFT); 1213 if (!msb->disk) { 1214 rc = -ENOMEM; 1215 goto out_release_id; 1216 } 1217 1218 msb->queue = blk_mq_init_sq_queue(&msb->tag_set, &mspro_mq_ops, 2, 1219 BLK_MQ_F_SHOULD_MERGE); 1220 if (IS_ERR(msb->queue)) { 1221 rc = PTR_ERR(msb->queue); 1222 msb->queue = NULL; 1223 goto out_put_disk; 1224 } 1225 1226 msb->queue->queuedata = card; 1227 1228 blk_queue_max_hw_sectors(msb->queue, MSPRO_BLOCK_MAX_PAGES); 1229 blk_queue_max_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS); 1230 blk_queue_max_segment_size(msb->queue, 1231 MSPRO_BLOCK_MAX_PAGES * msb->page_size); 1232 1233 msb->disk->major = major; 1234 msb->disk->first_minor = disk_id << MSPRO_BLOCK_PART_SHIFT; 1235 msb->disk->fops = &ms_block_bdops; 1236 msb->usage_count = 1; 1237 msb->disk->private_data = msb; 1238 msb->disk->queue = msb->queue; 1239 1240 sprintf(msb->disk->disk_name, "mspblk%d", disk_id); 1241 1242 blk_queue_logical_block_size(msb->queue, msb->page_size); 1243 1244 capacity = be16_to_cpu(sys_info->user_block_count); 1245 capacity *= be16_to_cpu(sys_info->block_size); 1246 capacity *= msb->page_size >> 9; 1247 set_capacity(msb->disk, capacity); 1248 dev_dbg(&card->dev, "capacity set %ld\n", capacity); 1249 1250 device_add_disk(&card->dev, msb->disk, NULL); 1251 msb->active = 1; 1252 return 0; 1253 1254 out_put_disk: 1255 put_disk(msb->disk); 1256 out_release_id: 1257 mutex_lock(&mspro_block_disk_lock); 1258 idr_remove(&mspro_block_disk_idr, disk_id); 1259 mutex_unlock(&mspro_block_disk_lock); 1260 return rc; 1261 } 1262 1263 static void mspro_block_data_clear(struct mspro_block_data *msb) 1264 { 1265 int cnt; 1266 struct mspro_sys_attr *s_attr; 1267 1268 if (msb->attr_group.attrs) { 1269 for (cnt = 0; msb->attr_group.attrs[cnt]; ++cnt) { 1270 s_attr = mspro_from_sysfs_attr(msb->attr_group 1271 .attrs[cnt]); 1272 kfree(s_attr->data); 1273 kfree(s_attr); 1274 } 1275 kfree(msb->attr_group.attrs); 1276 } 1277 1278 msb->card = NULL; 1279 } 1280 1281 static int mspro_block_check_card(struct memstick_dev *card) 1282 { 1283 struct mspro_block_data *msb = memstick_get_drvdata(card); 1284 1285 return (msb->active == 1); 1286 } 1287 1288 static int mspro_block_probe(struct memstick_dev *card) 1289 { 1290 struct mspro_block_data *msb; 1291 int rc = 0; 1292 1293 msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL); 1294 if (!msb) 1295 return -ENOMEM; 1296 memstick_set_drvdata(card, msb); 1297 msb->card = card; 1298 spin_lock_init(&msb->q_lock); 1299 1300 rc = mspro_block_init_card(card); 1301 1302 if (rc) 1303 goto out_free; 1304 1305 rc = sysfs_create_group(&card->dev.kobj, &msb->attr_group); 1306 if (rc) 1307 goto out_free; 1308 1309 rc = mspro_block_init_disk(card); 1310 if (!rc) { 1311 card->check = mspro_block_check_card; 1312 card->stop = mspro_block_stop; 1313 card->start = mspro_block_start; 1314 return 0; 1315 } 1316 1317 sysfs_remove_group(&card->dev.kobj, &msb->attr_group); 1318 out_free: 1319 memstick_set_drvdata(card, NULL); 1320 mspro_block_data_clear(msb); 1321 kfree(msb); 1322 return rc; 1323 } 1324 1325 static void mspro_block_remove(struct memstick_dev *card) 1326 { 1327 struct mspro_block_data *msb = memstick_get_drvdata(card); 1328 unsigned long flags; 1329 1330 spin_lock_irqsave(&msb->q_lock, flags); 1331 msb->eject = 1; 1332 spin_unlock_irqrestore(&msb->q_lock, flags); 1333 blk_mq_start_hw_queues(msb->queue); 1334 1335 del_gendisk(msb->disk); 1336 dev_dbg(&card->dev, "mspro block remove\n"); 1337 1338 blk_cleanup_queue(msb->queue); 1339 blk_mq_free_tag_set(&msb->tag_set); 1340 msb->queue = NULL; 1341 1342 sysfs_remove_group(&card->dev.kobj, &msb->attr_group); 1343 1344 mutex_lock(&mspro_block_disk_lock); 1345 mspro_block_data_clear(msb); 1346 mutex_unlock(&mspro_block_disk_lock); 1347 1348 mspro_block_disk_release(msb->disk); 1349 memstick_set_drvdata(card, NULL); 1350 } 1351 1352 #ifdef CONFIG_PM 1353 1354 static int mspro_block_suspend(struct memstick_dev *card, pm_message_t state) 1355 { 1356 struct mspro_block_data *msb = memstick_get_drvdata(card); 1357 unsigned long flags; 1358 1359 blk_mq_stop_hw_queues(msb->queue); 1360 1361 spin_lock_irqsave(&msb->q_lock, flags); 1362 msb->active = 0; 1363 spin_unlock_irqrestore(&msb->q_lock, flags); 1364 1365 return 0; 1366 } 1367 1368 static int mspro_block_resume(struct memstick_dev *card) 1369 { 1370 struct mspro_block_data *msb = memstick_get_drvdata(card); 1371 int rc = 0; 1372 1373 #ifdef CONFIG_MEMSTICK_UNSAFE_RESUME 1374 1375 struct mspro_block_data *new_msb; 1376 struct memstick_host *host = card->host; 1377 struct mspro_sys_attr *s_attr, *r_attr; 1378 unsigned char cnt; 1379 1380 mutex_lock(&host->lock); 1381 new_msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL); 1382 if (!new_msb) { 1383 rc = -ENOMEM; 1384 goto out_unlock; 1385 } 1386 1387 new_msb->card = card; 1388 memstick_set_drvdata(card, new_msb); 1389 if (mspro_block_init_card(card)) 1390 goto out_free; 1391 1392 for (cnt = 0; new_msb->attr_group.attrs[cnt] 1393 && msb->attr_group.attrs[cnt]; ++cnt) { 1394 s_attr = mspro_from_sysfs_attr(new_msb->attr_group.attrs[cnt]); 1395 r_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[cnt]); 1396 1397 if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO 1398 && r_attr->id == s_attr->id) { 1399 if (memcmp(s_attr->data, r_attr->data, s_attr->size)) 1400 break; 1401 1402 msb->active = 1; 1403 break; 1404 } 1405 } 1406 1407 out_free: 1408 memstick_set_drvdata(card, msb); 1409 mspro_block_data_clear(new_msb); 1410 kfree(new_msb); 1411 out_unlock: 1412 mutex_unlock(&host->lock); 1413 1414 #endif /* CONFIG_MEMSTICK_UNSAFE_RESUME */ 1415 1416 blk_mq_start_hw_queues(msb->queue); 1417 return rc; 1418 } 1419 1420 #else 1421 1422 #define mspro_block_suspend NULL 1423 #define mspro_block_resume NULL 1424 1425 #endif /* CONFIG_PM */ 1426 1427 static struct memstick_device_id mspro_block_id_tbl[] = { 1428 {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_PRO, MEMSTICK_CATEGORY_STORAGE_DUO, 1429 MEMSTICK_CLASS_DUO}, 1430 {} 1431 }; 1432 1433 1434 static struct memstick_driver mspro_block_driver = { 1435 .driver = { 1436 .name = DRIVER_NAME, 1437 .owner = THIS_MODULE 1438 }, 1439 .id_table = mspro_block_id_tbl, 1440 .probe = mspro_block_probe, 1441 .remove = mspro_block_remove, 1442 .suspend = mspro_block_suspend, 1443 .resume = mspro_block_resume 1444 }; 1445 1446 static int __init mspro_block_init(void) 1447 { 1448 int rc = -ENOMEM; 1449 1450 rc = register_blkdev(major, DRIVER_NAME); 1451 if (rc < 0) { 1452 printk(KERN_ERR DRIVER_NAME ": failed to register " 1453 "major %d, error %d\n", major, rc); 1454 return rc; 1455 } 1456 if (!major) 1457 major = rc; 1458 1459 rc = memstick_register_driver(&mspro_block_driver); 1460 if (rc) 1461 unregister_blkdev(major, DRIVER_NAME); 1462 return rc; 1463 } 1464 1465 static void __exit mspro_block_exit(void) 1466 { 1467 memstick_unregister_driver(&mspro_block_driver); 1468 unregister_blkdev(major, DRIVER_NAME); 1469 idr_destroy(&mspro_block_disk_idr); 1470 } 1471 1472 module_init(mspro_block_init); 1473 module_exit(mspro_block_exit); 1474 1475 MODULE_LICENSE("GPL"); 1476 MODULE_AUTHOR("Alex Dubov"); 1477 MODULE_DESCRIPTION("Sony MemoryStickPro block device driver"); 1478 MODULE_DEVICE_TABLE(memstick, mspro_block_id_tbl); 1479