mspro_block.c 39 KB

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