mspro_block.c 38 KB

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