mspro_block.c 39 KB

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