mspro_block.c 39 KB

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