mspro_block.c 39 KB

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