ub.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669
  1. /*
  2. * The low performance USB storage driver (ub).
  3. *
  4. * Copyright (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
  5. * Copyright (C) 2004 Pete Zaitcev (zaitcev@yahoo.com)
  6. *
  7. * This work is a part of Linux kernel, is derived from it,
  8. * and is not licensed separately. See file COPYING for details.
  9. *
  10. * TODO (sorted by decreasing priority)
  11. * -- Kill first_open (Al Viro fixed the block layer now)
  12. * -- set readonly flag for CDs, set removable flag for CF readers
  13. * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
  14. * -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
  15. * -- verify the 13 conditions and do bulk resets
  16. * -- kill last_pipe and simply do two-state clearing on both pipes
  17. * -- verify protocol (bulk) from USB descriptors (maybe...)
  18. * -- highmem
  19. * -- move top_sense and work_bcs into separate allocations (if they survive)
  20. * for cache purists and esoteric architectures.
  21. * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
  22. * -- prune comments, they are too volumnous
  23. * -- Exterminate P3 printks
  24. * -- Resove XXX's
  25. * -- Redo "benh's retries", perhaps have spin-up code to handle them. V:D=?
  26. * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/usb.h>
  31. #include <linux/usb_usual.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/devfs_fs_kernel.h>
  34. #include <linux/timer.h>
  35. #include <scsi/scsi.h>
  36. #define DRV_NAME "ub"
  37. #define DEVFS_NAME DRV_NAME
  38. #define UB_MAJOR 180
  39. /*
  40. * The command state machine is the key model for understanding of this driver.
  41. *
  42. * The general rule is that all transitions are done towards the bottom
  43. * of the diagram, thus preventing any loops.
  44. *
  45. * An exception to that is how the STAT state is handled. A counter allows it
  46. * to be re-entered along the path marked with [C].
  47. *
  48. * +--------+
  49. * ! INIT !
  50. * +--------+
  51. * !
  52. * ub_scsi_cmd_start fails ->--------------------------------------\
  53. * ! !
  54. * V !
  55. * +--------+ !
  56. * ! CMD ! !
  57. * +--------+ !
  58. * ! +--------+ !
  59. * was -EPIPE -->-------------------------------->! CLEAR ! !
  60. * ! +--------+ !
  61. * ! ! !
  62. * was error -->------------------------------------- ! --------->\
  63. * ! ! !
  64. * /--<-- cmd->dir == NONE ? ! !
  65. * ! ! ! !
  66. * ! V ! !
  67. * ! +--------+ ! !
  68. * ! ! DATA ! ! !
  69. * ! +--------+ ! !
  70. * ! ! +---------+ ! !
  71. * ! was -EPIPE -->--------------->! CLR2STS ! ! !
  72. * ! ! +---------+ ! !
  73. * ! ! ! ! !
  74. * ! ! was error -->---- ! --------->\
  75. * ! was error -->--------------------- ! ------------- ! --------->\
  76. * ! ! ! ! !
  77. * ! V ! ! !
  78. * \--->+--------+ ! ! !
  79. * ! STAT !<--------------------------/ ! !
  80. * /--->+--------+ ! !
  81. * ! ! ! !
  82. * [C] was -EPIPE -->-----------\ ! !
  83. * ! ! ! ! !
  84. * +<---- len == 0 ! ! !
  85. * ! ! ! ! !
  86. * ! was error -->--------------------------------------!---------->\
  87. * ! ! ! ! !
  88. * +<---- bad CSW ! ! !
  89. * +<---- bad tag ! ! !
  90. * ! ! V ! !
  91. * ! ! +--------+ ! !
  92. * ! ! ! CLRRS ! ! !
  93. * ! ! +--------+ ! !
  94. * ! ! ! ! !
  95. * \------- ! --------------------[C]--------\ ! !
  96. * ! ! ! !
  97. * cmd->error---\ +--------+ ! !
  98. * ! +--------------->! SENSE !<----------/ !
  99. * STAT_FAIL----/ +--------+ !
  100. * ! ! V
  101. * ! V +--------+
  102. * \--------------------------------\--------------------->! DONE !
  103. * +--------+
  104. */
  105. /*
  106. * This many LUNs per USB device.
  107. * Every one of them takes a host, see UB_MAX_HOSTS.
  108. */
  109. #define UB_MAX_LUNS 9
  110. /*
  111. */
  112. #define UB_PARTS_PER_LUN 8
  113. #define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
  114. #define UB_SENSE_SIZE 18
  115. /*
  116. */
  117. /* command block wrapper */
  118. struct bulk_cb_wrap {
  119. __le32 Signature; /* contains 'USBC' */
  120. u32 Tag; /* unique per command id */
  121. __le32 DataTransferLength; /* size of data */
  122. u8 Flags; /* direction in bit 0 */
  123. u8 Lun; /* LUN */
  124. u8 Length; /* of of the CDB */
  125. u8 CDB[UB_MAX_CDB_SIZE]; /* max command */
  126. };
  127. #define US_BULK_CB_WRAP_LEN 31
  128. #define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
  129. #define US_BULK_FLAG_IN 1
  130. #define US_BULK_FLAG_OUT 0
  131. /* command status wrapper */
  132. struct bulk_cs_wrap {
  133. __le32 Signature; /* should = 'USBS' */
  134. u32 Tag; /* same as original command */
  135. __le32 Residue; /* amount not transferred */
  136. u8 Status; /* see below */
  137. };
  138. #define US_BULK_CS_WRAP_LEN 13
  139. #define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
  140. #define US_BULK_STAT_OK 0
  141. #define US_BULK_STAT_FAIL 1
  142. #define US_BULK_STAT_PHASE 2
  143. /* bulk-only class specific requests */
  144. #define US_BULK_RESET_REQUEST 0xff
  145. #define US_BULK_GET_MAX_LUN 0xfe
  146. /*
  147. */
  148. struct ub_dev;
  149. #define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */
  150. #define UB_MAX_SECTORS 64
  151. /*
  152. * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
  153. * even if a webcam hogs the bus, but some devices need time to spin up.
  154. */
  155. #define UB_URB_TIMEOUT (HZ*2)
  156. #define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
  157. #define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
  158. #define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
  159. /*
  160. * An instance of a SCSI command in transit.
  161. */
  162. #define UB_DIR_NONE 0
  163. #define UB_DIR_READ 1
  164. #define UB_DIR_ILLEGAL2 2
  165. #define UB_DIR_WRITE 3
  166. #define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
  167. (((c)==UB_DIR_READ)? 'r': 'n'))
  168. enum ub_scsi_cmd_state {
  169. UB_CMDST_INIT, /* Initial state */
  170. UB_CMDST_CMD, /* Command submitted */
  171. UB_CMDST_DATA, /* Data phase */
  172. UB_CMDST_CLR2STS, /* Clearing before requesting status */
  173. UB_CMDST_STAT, /* Status phase */
  174. UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */
  175. UB_CMDST_CLRRS, /* Clearing before retrying status */
  176. UB_CMDST_SENSE, /* Sending Request Sense */
  177. UB_CMDST_DONE /* Final state */
  178. };
  179. static char *ub_scsi_cmd_stname[] = {
  180. ". ",
  181. "Cmd",
  182. "dat",
  183. "c2s",
  184. "sts",
  185. "clr",
  186. "crs",
  187. "Sen",
  188. "fin"
  189. };
  190. struct ub_scsi_cmd {
  191. unsigned char cdb[UB_MAX_CDB_SIZE];
  192. unsigned char cdb_len;
  193. unsigned char dir; /* 0 - none, 1 - read, 3 - write. */
  194. unsigned char trace_index;
  195. enum ub_scsi_cmd_state state;
  196. unsigned int tag;
  197. struct ub_scsi_cmd *next;
  198. int error; /* Return code - valid upon done */
  199. unsigned int act_len; /* Return size */
  200. unsigned char key, asc, ascq; /* May be valid if error==-EIO */
  201. int stat_count; /* Retries getting status. */
  202. unsigned int len; /* Requested length */
  203. unsigned int current_sg;
  204. unsigned int nsg; /* sgv[nsg] */
  205. struct scatterlist sgv[UB_MAX_REQ_SG];
  206. struct ub_lun *lun;
  207. void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
  208. void *back;
  209. };
  210. struct ub_request {
  211. struct request *rq;
  212. unsigned int current_try;
  213. unsigned int nsg; /* sgv[nsg] */
  214. struct scatterlist sgv[UB_MAX_REQ_SG];
  215. };
  216. /*
  217. */
  218. struct ub_capacity {
  219. unsigned long nsec; /* Linux size - 512 byte sectors */
  220. unsigned int bsize; /* Linux hardsect_size */
  221. unsigned int bshift; /* Shift between 512 and hard sects */
  222. };
  223. /*
  224. * The SCSI command tracing structure.
  225. */
  226. #define SCMD_ST_HIST_SZ 8
  227. #define SCMD_TRACE_SZ 63 /* Less than 4KB of 61-byte lines */
  228. struct ub_scsi_cmd_trace {
  229. int hcur;
  230. unsigned int tag;
  231. unsigned int req_size, act_size;
  232. unsigned char op;
  233. unsigned char dir;
  234. unsigned char key, asc, ascq;
  235. char st_hst[SCMD_ST_HIST_SZ];
  236. };
  237. struct ub_scsi_trace {
  238. int cur;
  239. struct ub_scsi_cmd_trace vec[SCMD_TRACE_SZ];
  240. };
  241. /*
  242. * This is a direct take-off from linux/include/completion.h
  243. * The difference is that I do not wait on this thing, just poll.
  244. * When I want to wait (ub_probe), I just use the stock completion.
  245. *
  246. * Note that INIT_COMPLETION takes no lock. It is correct. But why
  247. * in the bloody hell that thing takes struct instead of pointer to struct
  248. * is quite beyond me. I just copied it from the stock completion.
  249. */
  250. struct ub_completion {
  251. unsigned int done;
  252. spinlock_t lock;
  253. };
  254. static inline void ub_init_completion(struct ub_completion *x)
  255. {
  256. x->done = 0;
  257. spin_lock_init(&x->lock);
  258. }
  259. #define UB_INIT_COMPLETION(x) ((x).done = 0)
  260. static void ub_complete(struct ub_completion *x)
  261. {
  262. unsigned long flags;
  263. spin_lock_irqsave(&x->lock, flags);
  264. x->done++;
  265. spin_unlock_irqrestore(&x->lock, flags);
  266. }
  267. static int ub_is_completed(struct ub_completion *x)
  268. {
  269. unsigned long flags;
  270. int ret;
  271. spin_lock_irqsave(&x->lock, flags);
  272. ret = x->done;
  273. spin_unlock_irqrestore(&x->lock, flags);
  274. return ret;
  275. }
  276. /*
  277. */
  278. struct ub_scsi_cmd_queue {
  279. int qlen, qmax;
  280. struct ub_scsi_cmd *head, *tail;
  281. };
  282. /*
  283. * The block device instance (one per LUN).
  284. */
  285. struct ub_lun {
  286. struct ub_dev *udev;
  287. struct list_head link;
  288. struct gendisk *disk;
  289. int id; /* Host index */
  290. int num; /* LUN number */
  291. char name[16];
  292. int changed; /* Media was changed */
  293. int removable;
  294. int readonly;
  295. int first_open; /* Kludge. See ub_bd_open. */
  296. struct ub_request urq;
  297. /* Use Ingo's mempool if or when we have more than one command. */
  298. /*
  299. * Currently we never need more than one command for the whole device.
  300. * However, giving every LUN a command is a cheap and automatic way
  301. * to enforce fairness between them.
  302. */
  303. int cmda[1];
  304. struct ub_scsi_cmd cmdv[1];
  305. struct ub_capacity capacity;
  306. };
  307. /*
  308. * The USB device instance.
  309. */
  310. struct ub_dev {
  311. spinlock_t lock;
  312. atomic_t poison; /* The USB device is disconnected */
  313. int openc; /* protected by ub_lock! */
  314. /* kref is too implicit for our taste */
  315. int reset; /* Reset is running */
  316. unsigned int tagcnt;
  317. char name[12];
  318. struct usb_device *dev;
  319. struct usb_interface *intf;
  320. struct list_head luns;
  321. unsigned int send_bulk_pipe; /* cached pipe values */
  322. unsigned int recv_bulk_pipe;
  323. unsigned int send_ctrl_pipe;
  324. unsigned int recv_ctrl_pipe;
  325. struct tasklet_struct tasklet;
  326. struct ub_scsi_cmd_queue cmd_queue;
  327. struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */
  328. unsigned char top_sense[UB_SENSE_SIZE];
  329. struct ub_completion work_done;
  330. struct urb work_urb;
  331. struct timer_list work_timer;
  332. int last_pipe; /* What might need clearing */
  333. __le32 signature; /* Learned signature */
  334. struct bulk_cb_wrap work_bcb;
  335. struct bulk_cs_wrap work_bcs;
  336. struct usb_ctrlrequest work_cr;
  337. struct work_struct reset_work;
  338. wait_queue_head_t reset_wait;
  339. int sg_stat[6];
  340. struct ub_scsi_trace tr;
  341. };
  342. /*
  343. */
  344. static void ub_cleanup(struct ub_dev *sc);
  345. static int ub_request_fn_1(struct ub_lun *lun, struct request *rq);
  346. static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
  347. struct ub_scsi_cmd *cmd, struct ub_request *urq);
  348. static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
  349. struct ub_scsi_cmd *cmd, struct ub_request *urq);
  350. static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  351. static void ub_end_rq(struct request *rq, int uptodate);
  352. static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
  353. struct ub_request *urq, struct ub_scsi_cmd *cmd);
  354. static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  355. static void ub_urb_complete(struct urb *urb, struct pt_regs *pt);
  356. static void ub_scsi_action(unsigned long _dev);
  357. static void ub_scsi_dispatch(struct ub_dev *sc);
  358. static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  359. static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  360. static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
  361. static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  362. static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  363. static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  364. static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  365. static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
  366. int stalled_pipe);
  367. static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
  368. static void ub_reset_enter(struct ub_dev *sc);
  369. static void ub_reset_task(void *arg);
  370. static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
  371. static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
  372. struct ub_capacity *ret);
  373. static int ub_probe_lun(struct ub_dev *sc, int lnum);
  374. /*
  375. */
  376. #ifdef CONFIG_USB_LIBUSUAL
  377. #define ub_usb_ids storage_usb_ids
  378. #else
  379. static struct usb_device_id ub_usb_ids[] = {
  380. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
  381. { }
  382. };
  383. MODULE_DEVICE_TABLE(usb, ub_usb_ids);
  384. #endif /* CONFIG_USB_LIBUSUAL */
  385. /*
  386. * Find me a way to identify "next free minor" for add_disk(),
  387. * and the array disappears the next day. However, the number of
  388. * hosts has something to do with the naming and /proc/partitions.
  389. * This has to be thought out in detail before changing.
  390. * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
  391. */
  392. #define UB_MAX_HOSTS 26
  393. static char ub_hostv[UB_MAX_HOSTS];
  394. static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */
  395. /*
  396. * The SCSI command tracing procedures.
  397. */
  398. static void ub_cmdtr_new(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  399. {
  400. int n;
  401. struct ub_scsi_cmd_trace *t;
  402. if ((n = sc->tr.cur + 1) == SCMD_TRACE_SZ) n = 0;
  403. t = &sc->tr.vec[n];
  404. memset(t, 0, sizeof(struct ub_scsi_cmd_trace));
  405. t->tag = cmd->tag;
  406. t->op = cmd->cdb[0];
  407. t->dir = cmd->dir;
  408. t->req_size = cmd->len;
  409. t->st_hst[0] = cmd->state;
  410. sc->tr.cur = n;
  411. cmd->trace_index = n;
  412. }
  413. static void ub_cmdtr_state(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  414. {
  415. int n;
  416. struct ub_scsi_cmd_trace *t;
  417. t = &sc->tr.vec[cmd->trace_index];
  418. if (t->tag == cmd->tag) {
  419. if ((n = t->hcur + 1) == SCMD_ST_HIST_SZ) n = 0;
  420. t->st_hst[n] = cmd->state;
  421. t->hcur = n;
  422. }
  423. }
  424. static void ub_cmdtr_act_len(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  425. {
  426. struct ub_scsi_cmd_trace *t;
  427. t = &sc->tr.vec[cmd->trace_index];
  428. if (t->tag == cmd->tag)
  429. t->act_size = cmd->act_len;
  430. }
  431. static void ub_cmdtr_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
  432. unsigned char *sense)
  433. {
  434. struct ub_scsi_cmd_trace *t;
  435. t = &sc->tr.vec[cmd->trace_index];
  436. if (t->tag == cmd->tag) {
  437. t->key = sense[2] & 0x0F;
  438. t->asc = sense[12];
  439. t->ascq = sense[13];
  440. }
  441. }
  442. static ssize_t ub_diag_show(struct device *dev, struct device_attribute *attr,
  443. char *page)
  444. {
  445. struct usb_interface *intf;
  446. struct ub_dev *sc;
  447. struct list_head *p;
  448. struct ub_lun *lun;
  449. int cnt;
  450. unsigned long flags;
  451. int nc, nh;
  452. int i, j;
  453. struct ub_scsi_cmd_trace *t;
  454. intf = to_usb_interface(dev);
  455. sc = usb_get_intfdata(intf);
  456. if (sc == NULL)
  457. return 0;
  458. cnt = 0;
  459. spin_lock_irqsave(&sc->lock, flags);
  460. cnt += sprintf(page + cnt,
  461. "poison %d reset %d\n",
  462. atomic_read(&sc->poison), sc->reset);
  463. cnt += sprintf(page + cnt,
  464. "qlen %d qmax %d\n",
  465. sc->cmd_queue.qlen, sc->cmd_queue.qmax);
  466. cnt += sprintf(page + cnt,
  467. "sg %d %d %d %d %d .. %d\n",
  468. sc->sg_stat[0],
  469. sc->sg_stat[1],
  470. sc->sg_stat[2],
  471. sc->sg_stat[3],
  472. sc->sg_stat[4],
  473. sc->sg_stat[5]);
  474. list_for_each (p, &sc->luns) {
  475. lun = list_entry(p, struct ub_lun, link);
  476. cnt += sprintf(page + cnt,
  477. "lun %u changed %d removable %d readonly %d\n",
  478. lun->num, lun->changed, lun->removable, lun->readonly);
  479. }
  480. if ((nc = sc->tr.cur + 1) == SCMD_TRACE_SZ) nc = 0;
  481. for (j = 0; j < SCMD_TRACE_SZ; j++) {
  482. t = &sc->tr.vec[nc];
  483. cnt += sprintf(page + cnt, "%08x %02x", t->tag, t->op);
  484. if (t->op == REQUEST_SENSE) {
  485. cnt += sprintf(page + cnt, " [sense %x %02x %02x]",
  486. t->key, t->asc, t->ascq);
  487. } else {
  488. cnt += sprintf(page + cnt, " %c", UB_DIR_CHAR(t->dir));
  489. cnt += sprintf(page + cnt, " [%5d %5d]",
  490. t->req_size, t->act_size);
  491. }
  492. if ((nh = t->hcur + 1) == SCMD_ST_HIST_SZ) nh = 0;
  493. for (i = 0; i < SCMD_ST_HIST_SZ; i++) {
  494. cnt += sprintf(page + cnt, " %s",
  495. ub_scsi_cmd_stname[(int)t->st_hst[nh]]);
  496. if (++nh == SCMD_ST_HIST_SZ) nh = 0;
  497. }
  498. cnt += sprintf(page + cnt, "\n");
  499. if (++nc == SCMD_TRACE_SZ) nc = 0;
  500. }
  501. spin_unlock_irqrestore(&sc->lock, flags);
  502. return cnt;
  503. }
  504. static DEVICE_ATTR(diag, S_IRUGO, ub_diag_show, NULL); /* N.B. World readable */
  505. /*
  506. * The id allocator.
  507. *
  508. * This also stores the host for indexing by minor, which is somewhat dirty.
  509. */
  510. static int ub_id_get(void)
  511. {
  512. unsigned long flags;
  513. int i;
  514. spin_lock_irqsave(&ub_lock, flags);
  515. for (i = 0; i < UB_MAX_HOSTS; i++) {
  516. if (ub_hostv[i] == 0) {
  517. ub_hostv[i] = 1;
  518. spin_unlock_irqrestore(&ub_lock, flags);
  519. return i;
  520. }
  521. }
  522. spin_unlock_irqrestore(&ub_lock, flags);
  523. return -1;
  524. }
  525. static void ub_id_put(int id)
  526. {
  527. unsigned long flags;
  528. if (id < 0 || id >= UB_MAX_HOSTS) {
  529. printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
  530. return;
  531. }
  532. spin_lock_irqsave(&ub_lock, flags);
  533. if (ub_hostv[id] == 0) {
  534. spin_unlock_irqrestore(&ub_lock, flags);
  535. printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
  536. return;
  537. }
  538. ub_hostv[id] = 0;
  539. spin_unlock_irqrestore(&ub_lock, flags);
  540. }
  541. /*
  542. * Downcount for deallocation. This rides on two assumptions:
  543. * - once something is poisoned, its refcount cannot grow
  544. * - opens cannot happen at this time (del_gendisk was done)
  545. * If the above is true, we can drop the lock, which we need for
  546. * blk_cleanup_queue(): the silly thing may attempt to sleep.
  547. * [Actually, it never needs to sleep for us, but it calls might_sleep()]
  548. */
  549. static void ub_put(struct ub_dev *sc)
  550. {
  551. unsigned long flags;
  552. spin_lock_irqsave(&ub_lock, flags);
  553. --sc->openc;
  554. if (sc->openc == 0 && atomic_read(&sc->poison)) {
  555. spin_unlock_irqrestore(&ub_lock, flags);
  556. ub_cleanup(sc);
  557. } else {
  558. spin_unlock_irqrestore(&ub_lock, flags);
  559. }
  560. }
  561. /*
  562. * Final cleanup and deallocation.
  563. */
  564. static void ub_cleanup(struct ub_dev *sc)
  565. {
  566. struct list_head *p;
  567. struct ub_lun *lun;
  568. request_queue_t *q;
  569. while (!list_empty(&sc->luns)) {
  570. p = sc->luns.next;
  571. lun = list_entry(p, struct ub_lun, link);
  572. list_del(p);
  573. /* I don't think queue can be NULL. But... Stolen from sx8.c */
  574. if ((q = lun->disk->queue) != NULL)
  575. blk_cleanup_queue(q);
  576. /*
  577. * If we zero disk->private_data BEFORE put_disk, we have
  578. * to check for NULL all over the place in open, release,
  579. * check_media and revalidate, because the block level
  580. * semaphore is well inside the put_disk.
  581. * But we cannot zero after the call, because *disk is gone.
  582. * The sd.c is blatantly racy in this area.
  583. */
  584. /* disk->private_data = NULL; */
  585. put_disk(lun->disk);
  586. lun->disk = NULL;
  587. ub_id_put(lun->id);
  588. kfree(lun);
  589. }
  590. kfree(sc);
  591. }
  592. /*
  593. * The "command allocator".
  594. */
  595. static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
  596. {
  597. struct ub_scsi_cmd *ret;
  598. if (lun->cmda[0])
  599. return NULL;
  600. ret = &lun->cmdv[0];
  601. lun->cmda[0] = 1;
  602. return ret;
  603. }
  604. static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
  605. {
  606. if (cmd != &lun->cmdv[0]) {
  607. printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
  608. lun->name, cmd);
  609. return;
  610. }
  611. if (!lun->cmda[0]) {
  612. printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
  613. return;
  614. }
  615. lun->cmda[0] = 0;
  616. }
  617. /*
  618. * The command queue.
  619. */
  620. static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  621. {
  622. struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
  623. if (t->qlen++ == 0) {
  624. t->head = cmd;
  625. t->tail = cmd;
  626. } else {
  627. t->tail->next = cmd;
  628. t->tail = cmd;
  629. }
  630. if (t->qlen > t->qmax)
  631. t->qmax = t->qlen;
  632. }
  633. static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  634. {
  635. struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
  636. if (t->qlen++ == 0) {
  637. t->head = cmd;
  638. t->tail = cmd;
  639. } else {
  640. cmd->next = t->head;
  641. t->head = cmd;
  642. }
  643. if (t->qlen > t->qmax)
  644. t->qmax = t->qlen;
  645. }
  646. static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
  647. {
  648. struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
  649. struct ub_scsi_cmd *cmd;
  650. if (t->qlen == 0)
  651. return NULL;
  652. if (--t->qlen == 0)
  653. t->tail = NULL;
  654. cmd = t->head;
  655. t->head = cmd->next;
  656. cmd->next = NULL;
  657. return cmd;
  658. }
  659. #define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
  660. /*
  661. * The request function is our main entry point
  662. */
  663. static void ub_request_fn(request_queue_t *q)
  664. {
  665. struct ub_lun *lun = q->queuedata;
  666. struct request *rq;
  667. while ((rq = elv_next_request(q)) != NULL) {
  668. if (ub_request_fn_1(lun, rq) != 0) {
  669. blk_stop_queue(q);
  670. break;
  671. }
  672. }
  673. }
  674. static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
  675. {
  676. struct ub_dev *sc = lun->udev;
  677. struct ub_scsi_cmd *cmd;
  678. struct ub_request *urq;
  679. int n_elem;
  680. if (atomic_read(&sc->poison) || lun->changed) {
  681. blkdev_dequeue_request(rq);
  682. ub_end_rq(rq, 0);
  683. return 0;
  684. }
  685. if (lun->urq.rq != NULL)
  686. return -1;
  687. if ((cmd = ub_get_cmd(lun)) == NULL)
  688. return -1;
  689. memset(cmd, 0, sizeof(struct ub_scsi_cmd));
  690. blkdev_dequeue_request(rq);
  691. urq = &lun->urq;
  692. memset(urq, 0, sizeof(struct ub_request));
  693. urq->rq = rq;
  694. /*
  695. * get scatterlist from block layer
  696. */
  697. n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
  698. if (n_elem < 0) {
  699. printk(KERN_INFO "%s: failed request map (%d)\n",
  700. lun->name, n_elem); /* P3 */
  701. goto drop;
  702. }
  703. if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
  704. printk(KERN_WARNING "%s: request with %d segments\n",
  705. lun->name, n_elem);
  706. goto drop;
  707. }
  708. urq->nsg = n_elem;
  709. sc->sg_stat[n_elem < 5 ? n_elem : 5]++;
  710. if (blk_pc_request(rq)) {
  711. ub_cmd_build_packet(sc, lun, cmd, urq);
  712. } else {
  713. ub_cmd_build_block(sc, lun, cmd, urq);
  714. }
  715. cmd->state = UB_CMDST_INIT;
  716. cmd->lun = lun;
  717. cmd->done = ub_rw_cmd_done;
  718. cmd->back = urq;
  719. cmd->tag = sc->tagcnt++;
  720. if (ub_submit_scsi(sc, cmd) != 0)
  721. goto drop;
  722. return 0;
  723. drop:
  724. ub_put_cmd(lun, cmd);
  725. ub_end_rq(rq, 0);
  726. return 0;
  727. }
  728. static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
  729. struct ub_scsi_cmd *cmd, struct ub_request *urq)
  730. {
  731. struct request *rq = urq->rq;
  732. unsigned int block, nblks;
  733. if (rq_data_dir(rq) == WRITE)
  734. cmd->dir = UB_DIR_WRITE;
  735. else
  736. cmd->dir = UB_DIR_READ;
  737. cmd->nsg = urq->nsg;
  738. memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
  739. /*
  740. * build the command
  741. *
  742. * The call to blk_queue_hardsect_size() guarantees that request
  743. * is aligned, but it is given in terms of 512 byte units, always.
  744. */
  745. block = rq->sector >> lun->capacity.bshift;
  746. nblks = rq->nr_sectors >> lun->capacity.bshift;
  747. cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10;
  748. /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
  749. cmd->cdb[2] = block >> 24;
  750. cmd->cdb[3] = block >> 16;
  751. cmd->cdb[4] = block >> 8;
  752. cmd->cdb[5] = block;
  753. cmd->cdb[7] = nblks >> 8;
  754. cmd->cdb[8] = nblks;
  755. cmd->cdb_len = 10;
  756. cmd->len = rq->nr_sectors * 512;
  757. }
  758. static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
  759. struct ub_scsi_cmd *cmd, struct ub_request *urq)
  760. {
  761. struct request *rq = urq->rq;
  762. if (rq->data_len == 0) {
  763. cmd->dir = UB_DIR_NONE;
  764. } else {
  765. if (rq_data_dir(rq) == WRITE)
  766. cmd->dir = UB_DIR_WRITE;
  767. else
  768. cmd->dir = UB_DIR_READ;
  769. }
  770. cmd->nsg = urq->nsg;
  771. memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
  772. memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
  773. cmd->cdb_len = rq->cmd_len;
  774. cmd->len = rq->data_len;
  775. }
  776. static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  777. {
  778. struct ub_lun *lun = cmd->lun;
  779. struct ub_request *urq = cmd->back;
  780. struct request *rq;
  781. int uptodate;
  782. rq = urq->rq;
  783. if (cmd->error == 0) {
  784. uptodate = 1;
  785. if (blk_pc_request(rq)) {
  786. if (cmd->act_len >= rq->data_len)
  787. rq->data_len = 0;
  788. else
  789. rq->data_len -= cmd->act_len;
  790. }
  791. } else {
  792. uptodate = 0;
  793. if (blk_pc_request(rq)) {
  794. /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
  795. memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
  796. rq->sense_len = UB_SENSE_SIZE;
  797. if (sc->top_sense[0] != 0)
  798. rq->errors = SAM_STAT_CHECK_CONDITION;
  799. else
  800. rq->errors = DID_ERROR << 16;
  801. } else {
  802. if (cmd->error == -EIO) {
  803. if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
  804. return;
  805. }
  806. }
  807. }
  808. urq->rq = NULL;
  809. ub_put_cmd(lun, cmd);
  810. ub_end_rq(rq, uptodate);
  811. blk_start_queue(lun->disk->queue);
  812. }
  813. static void ub_end_rq(struct request *rq, int uptodate)
  814. {
  815. end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
  816. end_that_request_last(rq, uptodate);
  817. }
  818. static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
  819. struct ub_request *urq, struct ub_scsi_cmd *cmd)
  820. {
  821. if (atomic_read(&sc->poison))
  822. return -ENXIO;
  823. ub_reset_enter(sc);
  824. if (urq->current_try >= 3)
  825. return -EIO;
  826. urq->current_try++;
  827. /* P3 */ printk("%s: dir %c len/act %d/%d "
  828. "[sense %x %02x %02x] retry %d\n",
  829. sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len,
  830. cmd->key, cmd->asc, cmd->ascq, urq->current_try);
  831. memset(cmd, 0, sizeof(struct ub_scsi_cmd));
  832. ub_cmd_build_block(sc, lun, cmd, urq);
  833. cmd->state = UB_CMDST_INIT;
  834. cmd->lun = lun;
  835. cmd->done = ub_rw_cmd_done;
  836. cmd->back = urq;
  837. cmd->tag = sc->tagcnt++;
  838. #if 0 /* Wasteful */
  839. return ub_submit_scsi(sc, cmd);
  840. #else
  841. ub_cmdq_add(sc, cmd);
  842. return 0;
  843. #endif
  844. }
  845. /*
  846. * Submit a regular SCSI operation (not an auto-sense).
  847. *
  848. * The Iron Law of Good Submit Routine is:
  849. * Zero return - callback is done, Nonzero return - callback is not done.
  850. * No exceptions.
  851. *
  852. * Host is assumed locked.
  853. *
  854. * XXX We only support Bulk for the moment.
  855. */
  856. static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  857. {
  858. if (cmd->state != UB_CMDST_INIT ||
  859. (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
  860. return -EINVAL;
  861. }
  862. ub_cmdq_add(sc, cmd);
  863. /*
  864. * We can call ub_scsi_dispatch(sc) right away here, but it's a little
  865. * safer to jump to a tasklet, in case upper layers do something silly.
  866. */
  867. tasklet_schedule(&sc->tasklet);
  868. return 0;
  869. }
  870. /*
  871. * Submit the first URB for the queued command.
  872. * This function does not deal with queueing in any way.
  873. */
  874. static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  875. {
  876. struct bulk_cb_wrap *bcb;
  877. int rc;
  878. bcb = &sc->work_bcb;
  879. /*
  880. * ``If the allocation length is eighteen or greater, and a device
  881. * server returns less than eithteen bytes of data, the application
  882. * client should assume that the bytes not transferred would have been
  883. * zeroes had the device server returned those bytes.''
  884. *
  885. * We zero sense for all commands so that when a packet request
  886. * fails it does not return a stale sense.
  887. */
  888. memset(&sc->top_sense, 0, UB_SENSE_SIZE);
  889. /* set up the command wrapper */
  890. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  891. bcb->Tag = cmd->tag; /* Endianness is not important */
  892. bcb->DataTransferLength = cpu_to_le32(cmd->len);
  893. bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
  894. bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
  895. bcb->Length = cmd->cdb_len;
  896. /* copy the command payload */
  897. memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
  898. UB_INIT_COMPLETION(sc->work_done);
  899. sc->last_pipe = sc->send_bulk_pipe;
  900. usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
  901. bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
  902. /* Fill what we shouldn't be filling, because usb-storage did so. */
  903. sc->work_urb.actual_length = 0;
  904. sc->work_urb.error_count = 0;
  905. sc->work_urb.status = 0;
  906. if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
  907. /* XXX Clear stalls */
  908. ub_complete(&sc->work_done);
  909. return rc;
  910. }
  911. sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
  912. add_timer(&sc->work_timer);
  913. cmd->state = UB_CMDST_CMD;
  914. ub_cmdtr_state(sc, cmd);
  915. return 0;
  916. }
  917. /*
  918. * Timeout handler.
  919. */
  920. static void ub_urb_timeout(unsigned long arg)
  921. {
  922. struct ub_dev *sc = (struct ub_dev *) arg;
  923. unsigned long flags;
  924. spin_lock_irqsave(&sc->lock, flags);
  925. usb_unlink_urb(&sc->work_urb);
  926. spin_unlock_irqrestore(&sc->lock, flags);
  927. }
  928. /*
  929. * Completion routine for the work URB.
  930. *
  931. * This can be called directly from usb_submit_urb (while we have
  932. * the sc->lock taken) and from an interrupt (while we do NOT have
  933. * the sc->lock taken). Therefore, bounce this off to a tasklet.
  934. */
  935. static void ub_urb_complete(struct urb *urb, struct pt_regs *pt)
  936. {
  937. struct ub_dev *sc = urb->context;
  938. ub_complete(&sc->work_done);
  939. tasklet_schedule(&sc->tasklet);
  940. }
  941. static void ub_scsi_action(unsigned long _dev)
  942. {
  943. struct ub_dev *sc = (struct ub_dev *) _dev;
  944. unsigned long flags;
  945. spin_lock_irqsave(&sc->lock, flags);
  946. del_timer(&sc->work_timer);
  947. ub_scsi_dispatch(sc);
  948. spin_unlock_irqrestore(&sc->lock, flags);
  949. }
  950. static void ub_scsi_dispatch(struct ub_dev *sc)
  951. {
  952. struct ub_scsi_cmd *cmd;
  953. int rc;
  954. while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) {
  955. if (cmd->state == UB_CMDST_DONE) {
  956. ub_cmdq_pop(sc);
  957. (*cmd->done)(sc, cmd);
  958. } else if (cmd->state == UB_CMDST_INIT) {
  959. ub_cmdtr_new(sc, cmd);
  960. if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
  961. break;
  962. cmd->error = rc;
  963. cmd->state = UB_CMDST_DONE;
  964. ub_cmdtr_state(sc, cmd);
  965. } else {
  966. if (!ub_is_completed(&sc->work_done))
  967. break;
  968. ub_scsi_urb_compl(sc, cmd);
  969. }
  970. }
  971. }
  972. static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  973. {
  974. struct urb *urb = &sc->work_urb;
  975. struct bulk_cs_wrap *bcs;
  976. int len;
  977. int rc;
  978. if (atomic_read(&sc->poison)) {
  979. ub_state_done(sc, cmd, -ENODEV);
  980. return;
  981. }
  982. if (cmd->state == UB_CMDST_CLEAR) {
  983. if (urb->status == -EPIPE) {
  984. /*
  985. * STALL while clearning STALL.
  986. * The control pipe clears itself - nothing to do.
  987. */
  988. printk(KERN_NOTICE "%s: stall on control pipe\n",
  989. sc->name);
  990. goto Bad_End;
  991. }
  992. /*
  993. * We ignore the result for the halt clear.
  994. */
  995. /* reset the endpoint toggle */
  996. usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
  997. usb_pipeout(sc->last_pipe), 0);
  998. ub_state_sense(sc, cmd);
  999. } else if (cmd->state == UB_CMDST_CLR2STS) {
  1000. if (urb->status == -EPIPE) {
  1001. printk(KERN_NOTICE "%s: stall on control pipe\n",
  1002. sc->name);
  1003. goto Bad_End;
  1004. }
  1005. /*
  1006. * We ignore the result for the halt clear.
  1007. */
  1008. /* reset the endpoint toggle */
  1009. usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
  1010. usb_pipeout(sc->last_pipe), 0);
  1011. ub_state_stat(sc, cmd);
  1012. } else if (cmd->state == UB_CMDST_CLRRS) {
  1013. if (urb->status == -EPIPE) {
  1014. printk(KERN_NOTICE "%s: stall on control pipe\n",
  1015. sc->name);
  1016. goto Bad_End;
  1017. }
  1018. /*
  1019. * We ignore the result for the halt clear.
  1020. */
  1021. /* reset the endpoint toggle */
  1022. usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
  1023. usb_pipeout(sc->last_pipe), 0);
  1024. ub_state_stat_counted(sc, cmd);
  1025. } else if (cmd->state == UB_CMDST_CMD) {
  1026. switch (urb->status) {
  1027. case 0:
  1028. break;
  1029. case -EOVERFLOW:
  1030. goto Bad_End;
  1031. case -EPIPE:
  1032. rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
  1033. if (rc != 0) {
  1034. printk(KERN_NOTICE "%s: "
  1035. "unable to submit clear (%d)\n",
  1036. sc->name, rc);
  1037. /*
  1038. * This is typically ENOMEM or some other such shit.
  1039. * Retrying is pointless. Just do Bad End on it...
  1040. */
  1041. ub_state_done(sc, cmd, rc);
  1042. return;
  1043. }
  1044. cmd->state = UB_CMDST_CLEAR;
  1045. ub_cmdtr_state(sc, cmd);
  1046. return;
  1047. case -ESHUTDOWN: /* unplug */
  1048. case -EILSEQ: /* unplug timeout on uhci */
  1049. ub_state_done(sc, cmd, -ENODEV);
  1050. return;
  1051. default:
  1052. goto Bad_End;
  1053. }
  1054. if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
  1055. goto Bad_End;
  1056. }
  1057. if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) {
  1058. ub_state_stat(sc, cmd);
  1059. return;
  1060. }
  1061. // udelay(125); // usb-storage has this
  1062. ub_data_start(sc, cmd);
  1063. } else if (cmd->state == UB_CMDST_DATA) {
  1064. if (urb->status == -EPIPE) {
  1065. rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
  1066. if (rc != 0) {
  1067. printk(KERN_NOTICE "%s: "
  1068. "unable to submit clear (%d)\n",
  1069. sc->name, rc);
  1070. ub_state_done(sc, cmd, rc);
  1071. return;
  1072. }
  1073. cmd->state = UB_CMDST_CLR2STS;
  1074. ub_cmdtr_state(sc, cmd);
  1075. return;
  1076. }
  1077. if (urb->status == -EOVERFLOW) {
  1078. /*
  1079. * A babble? Failure, but we must transfer CSW now.
  1080. */
  1081. cmd->error = -EOVERFLOW; /* A cheap trick... */
  1082. ub_state_stat(sc, cmd);
  1083. return;
  1084. }
  1085. if (cmd->dir == UB_DIR_WRITE) {
  1086. /*
  1087. * Do not continue writes in case of a failure.
  1088. * Doing so would cause sectors to be mixed up,
  1089. * which is worse than sectors lost.
  1090. *
  1091. * We must try to read the CSW, or many devices
  1092. * get confused.
  1093. */
  1094. len = urb->actual_length;
  1095. if (urb->status != 0 ||
  1096. len != cmd->sgv[cmd->current_sg].length) {
  1097. cmd->act_len += len;
  1098. ub_cmdtr_act_len(sc, cmd);
  1099. cmd->error = -EIO;
  1100. ub_state_stat(sc, cmd);
  1101. return;
  1102. }
  1103. } else {
  1104. /*
  1105. * If an error occurs on read, we record it, and
  1106. * continue to fetch data in order to avoid bubble.
  1107. *
  1108. * As a small shortcut, we stop if we detect that
  1109. * a CSW mixed into data.
  1110. */
  1111. if (urb->status != 0)
  1112. cmd->error = -EIO;
  1113. len = urb->actual_length;
  1114. if (urb->status != 0 ||
  1115. len != cmd->sgv[cmd->current_sg].length) {
  1116. if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
  1117. goto Bad_End;
  1118. }
  1119. }
  1120. cmd->act_len += urb->actual_length;
  1121. ub_cmdtr_act_len(sc, cmd);
  1122. if (++cmd->current_sg < cmd->nsg) {
  1123. ub_data_start(sc, cmd);
  1124. return;
  1125. }
  1126. ub_state_stat(sc, cmd);
  1127. } else if (cmd->state == UB_CMDST_STAT) {
  1128. if (urb->status == -EPIPE) {
  1129. rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
  1130. if (rc != 0) {
  1131. printk(KERN_NOTICE "%s: "
  1132. "unable to submit clear (%d)\n",
  1133. sc->name, rc);
  1134. ub_state_done(sc, cmd, rc);
  1135. return;
  1136. }
  1137. /*
  1138. * Having a stall when getting CSW is an error, so
  1139. * make sure uppper levels are not oblivious to it.
  1140. */
  1141. cmd->error = -EIO; /* A cheap trick... */
  1142. cmd->state = UB_CMDST_CLRRS;
  1143. ub_cmdtr_state(sc, cmd);
  1144. return;
  1145. }
  1146. /* Catch everything, including -EOVERFLOW and other nasties. */
  1147. if (urb->status != 0)
  1148. goto Bad_End;
  1149. if (urb->actual_length == 0) {
  1150. ub_state_stat_counted(sc, cmd);
  1151. return;
  1152. }
  1153. /*
  1154. * Check the returned Bulk protocol status.
  1155. * The status block has to be validated first.
  1156. */
  1157. bcs = &sc->work_bcs;
  1158. if (sc->signature == cpu_to_le32(0)) {
  1159. /*
  1160. * This is the first reply, so do not perform the check.
  1161. * Instead, remember the signature the device uses
  1162. * for future checks. But do not allow a nul.
  1163. */
  1164. sc->signature = bcs->Signature;
  1165. if (sc->signature == cpu_to_le32(0)) {
  1166. ub_state_stat_counted(sc, cmd);
  1167. return;
  1168. }
  1169. } else {
  1170. if (bcs->Signature != sc->signature) {
  1171. ub_state_stat_counted(sc, cmd);
  1172. return;
  1173. }
  1174. }
  1175. if (bcs->Tag != cmd->tag) {
  1176. /*
  1177. * This usually happens when we disagree with the
  1178. * device's microcode about something. For instance,
  1179. * a few of them throw this after timeouts. They buffer
  1180. * commands and reply at commands we timed out before.
  1181. * Without flushing these replies we loop forever.
  1182. */
  1183. ub_state_stat_counted(sc, cmd);
  1184. return;
  1185. }
  1186. len = le32_to_cpu(bcs->Residue);
  1187. if (len != cmd->len - cmd->act_len) {
  1188. /*
  1189. * It is all right to transfer less, the caller has
  1190. * to check. But it's not all right if the device
  1191. * counts disagree with our counts.
  1192. */
  1193. /* P3 */ printk("%s: resid %d len %d act %d\n",
  1194. sc->name, len, cmd->len, cmd->act_len);
  1195. goto Bad_End;
  1196. }
  1197. switch (bcs->Status) {
  1198. case US_BULK_STAT_OK:
  1199. break;
  1200. case US_BULK_STAT_FAIL:
  1201. ub_state_sense(sc, cmd);
  1202. return;
  1203. case US_BULK_STAT_PHASE:
  1204. /* P3 */ printk("%s: status PHASE\n", sc->name);
  1205. goto Bad_End;
  1206. default:
  1207. printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
  1208. sc->name, bcs->Status);
  1209. ub_state_done(sc, cmd, -EINVAL);
  1210. return;
  1211. }
  1212. /* Not zeroing error to preserve a babble indicator */
  1213. if (cmd->error != 0) {
  1214. ub_state_sense(sc, cmd);
  1215. return;
  1216. }
  1217. cmd->state = UB_CMDST_DONE;
  1218. ub_cmdtr_state(sc, cmd);
  1219. ub_cmdq_pop(sc);
  1220. (*cmd->done)(sc, cmd);
  1221. } else if (cmd->state == UB_CMDST_SENSE) {
  1222. ub_state_done(sc, cmd, -EIO);
  1223. } else {
  1224. printk(KERN_WARNING "%s: "
  1225. "wrong command state %d\n",
  1226. sc->name, cmd->state);
  1227. ub_state_done(sc, cmd, -EINVAL);
  1228. return;
  1229. }
  1230. return;
  1231. Bad_End: /* Little Excel is dead */
  1232. ub_state_done(sc, cmd, -EIO);
  1233. }
  1234. /*
  1235. * Factorization helper for the command state machine:
  1236. * Initiate a data segment transfer.
  1237. */
  1238. static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1239. {
  1240. struct scatterlist *sg = &cmd->sgv[cmd->current_sg];
  1241. int pipe;
  1242. int rc;
  1243. UB_INIT_COMPLETION(sc->work_done);
  1244. if (cmd->dir == UB_DIR_READ)
  1245. pipe = sc->recv_bulk_pipe;
  1246. else
  1247. pipe = sc->send_bulk_pipe;
  1248. sc->last_pipe = pipe;
  1249. usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe,
  1250. page_address(sg->page) + sg->offset, sg->length,
  1251. ub_urb_complete, sc);
  1252. sc->work_urb.actual_length = 0;
  1253. sc->work_urb.error_count = 0;
  1254. sc->work_urb.status = 0;
  1255. if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
  1256. /* XXX Clear stalls */
  1257. ub_complete(&sc->work_done);
  1258. ub_state_done(sc, cmd, rc);
  1259. return;
  1260. }
  1261. sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
  1262. add_timer(&sc->work_timer);
  1263. cmd->state = UB_CMDST_DATA;
  1264. ub_cmdtr_state(sc, cmd);
  1265. }
  1266. /*
  1267. * Factorization helper for the command state machine:
  1268. * Finish the command.
  1269. */
  1270. static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
  1271. {
  1272. cmd->error = rc;
  1273. cmd->state = UB_CMDST_DONE;
  1274. ub_cmdtr_state(sc, cmd);
  1275. ub_cmdq_pop(sc);
  1276. (*cmd->done)(sc, cmd);
  1277. }
  1278. /*
  1279. * Factorization helper for the command state machine:
  1280. * Submit a CSW read.
  1281. */
  1282. static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1283. {
  1284. int rc;
  1285. UB_INIT_COMPLETION(sc->work_done);
  1286. sc->last_pipe = sc->recv_bulk_pipe;
  1287. usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
  1288. &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
  1289. sc->work_urb.actual_length = 0;
  1290. sc->work_urb.error_count = 0;
  1291. sc->work_urb.status = 0;
  1292. if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
  1293. /* XXX Clear stalls */
  1294. ub_complete(&sc->work_done);
  1295. ub_state_done(sc, cmd, rc);
  1296. return -1;
  1297. }
  1298. sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
  1299. add_timer(&sc->work_timer);
  1300. return 0;
  1301. }
  1302. /*
  1303. * Factorization helper for the command state machine:
  1304. * Submit a CSW read and go to STAT state.
  1305. */
  1306. static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1307. {
  1308. if (__ub_state_stat(sc, cmd) != 0)
  1309. return;
  1310. cmd->stat_count = 0;
  1311. cmd->state = UB_CMDST_STAT;
  1312. ub_cmdtr_state(sc, cmd);
  1313. }
  1314. /*
  1315. * Factorization helper for the command state machine:
  1316. * Submit a CSW read and go to STAT state with counter (along [C] path).
  1317. */
  1318. static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1319. {
  1320. if (++cmd->stat_count >= 4) {
  1321. ub_state_sense(sc, cmd);
  1322. return;
  1323. }
  1324. if (__ub_state_stat(sc, cmd) != 0)
  1325. return;
  1326. cmd->state = UB_CMDST_STAT;
  1327. ub_cmdtr_state(sc, cmd);
  1328. }
  1329. /*
  1330. * Factorization helper for the command state machine:
  1331. * Submit a REQUEST SENSE and go to SENSE state.
  1332. */
  1333. static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1334. {
  1335. struct ub_scsi_cmd *scmd;
  1336. struct scatterlist *sg;
  1337. int rc;
  1338. if (cmd->cdb[0] == REQUEST_SENSE) {
  1339. rc = -EPIPE;
  1340. goto error;
  1341. }
  1342. scmd = &sc->top_rqs_cmd;
  1343. memset(scmd, 0, sizeof(struct ub_scsi_cmd));
  1344. scmd->cdb[0] = REQUEST_SENSE;
  1345. scmd->cdb[4] = UB_SENSE_SIZE;
  1346. scmd->cdb_len = 6;
  1347. scmd->dir = UB_DIR_READ;
  1348. scmd->state = UB_CMDST_INIT;
  1349. scmd->nsg = 1;
  1350. sg = &scmd->sgv[0];
  1351. sg->page = virt_to_page(sc->top_sense);
  1352. sg->offset = (unsigned long)sc->top_sense & (PAGE_SIZE-1);
  1353. sg->length = UB_SENSE_SIZE;
  1354. scmd->len = UB_SENSE_SIZE;
  1355. scmd->lun = cmd->lun;
  1356. scmd->done = ub_top_sense_done;
  1357. scmd->back = cmd;
  1358. scmd->tag = sc->tagcnt++;
  1359. cmd->state = UB_CMDST_SENSE;
  1360. ub_cmdtr_state(sc, cmd);
  1361. ub_cmdq_insert(sc, scmd);
  1362. return;
  1363. error:
  1364. ub_state_done(sc, cmd, rc);
  1365. }
  1366. /*
  1367. * A helper for the command's state machine:
  1368. * Submit a stall clear.
  1369. */
  1370. static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
  1371. int stalled_pipe)
  1372. {
  1373. int endp;
  1374. struct usb_ctrlrequest *cr;
  1375. int rc;
  1376. endp = usb_pipeendpoint(stalled_pipe);
  1377. if (usb_pipein (stalled_pipe))
  1378. endp |= USB_DIR_IN;
  1379. cr = &sc->work_cr;
  1380. cr->bRequestType = USB_RECIP_ENDPOINT;
  1381. cr->bRequest = USB_REQ_CLEAR_FEATURE;
  1382. cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
  1383. cr->wIndex = cpu_to_le16(endp);
  1384. cr->wLength = cpu_to_le16(0);
  1385. UB_INIT_COMPLETION(sc->work_done);
  1386. usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
  1387. (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
  1388. sc->work_urb.actual_length = 0;
  1389. sc->work_urb.error_count = 0;
  1390. sc->work_urb.status = 0;
  1391. if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
  1392. ub_complete(&sc->work_done);
  1393. return rc;
  1394. }
  1395. sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
  1396. add_timer(&sc->work_timer);
  1397. return 0;
  1398. }
  1399. /*
  1400. */
  1401. static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
  1402. {
  1403. unsigned char *sense = sc->top_sense;
  1404. struct ub_scsi_cmd *cmd;
  1405. /*
  1406. * Ignoring scmd->act_len, because the buffer was pre-zeroed.
  1407. */
  1408. ub_cmdtr_sense(sc, scmd, sense);
  1409. /*
  1410. * Find the command which triggered the unit attention or a check,
  1411. * save the sense into it, and advance its state machine.
  1412. */
  1413. if ((cmd = ub_cmdq_peek(sc)) == NULL) {
  1414. printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
  1415. return;
  1416. }
  1417. if (cmd != scmd->back) {
  1418. printk(KERN_WARNING "%s: "
  1419. "sense done for wrong command 0x%x\n",
  1420. sc->name, cmd->tag);
  1421. return;
  1422. }
  1423. if (cmd->state != UB_CMDST_SENSE) {
  1424. printk(KERN_WARNING "%s: "
  1425. "sense done with bad cmd state %d\n",
  1426. sc->name, cmd->state);
  1427. return;
  1428. }
  1429. cmd->key = sense[2] & 0x0F;
  1430. cmd->asc = sense[12];
  1431. cmd->ascq = sense[13];
  1432. ub_scsi_urb_compl(sc, cmd);
  1433. }
  1434. /*
  1435. * Reset management
  1436. */
  1437. static void ub_reset_enter(struct ub_dev *sc)
  1438. {
  1439. if (sc->reset) {
  1440. /* This happens often on multi-LUN devices. */
  1441. return;
  1442. }
  1443. sc->reset = 1;
  1444. #if 0 /* Not needed because the disconnect waits for us. */
  1445. unsigned long flags;
  1446. spin_lock_irqsave(&ub_lock, flags);
  1447. sc->openc++;
  1448. spin_unlock_irqrestore(&ub_lock, flags);
  1449. #endif
  1450. #if 0 /* We let them stop themselves. */
  1451. struct list_head *p;
  1452. struct ub_lun *lun;
  1453. list_for_each(p, &sc->luns) {
  1454. lun = list_entry(p, struct ub_lun, link);
  1455. blk_stop_queue(lun->disk->queue);
  1456. }
  1457. #endif
  1458. schedule_work(&sc->reset_work);
  1459. }
  1460. static void ub_reset_task(void *arg)
  1461. {
  1462. struct ub_dev *sc = arg;
  1463. unsigned long flags;
  1464. struct list_head *p;
  1465. struct ub_lun *lun;
  1466. int lkr, rc;
  1467. if (!sc->reset) {
  1468. printk(KERN_WARNING "%s: Running reset unrequested\n",
  1469. sc->name);
  1470. return;
  1471. }
  1472. if (atomic_read(&sc->poison)) {
  1473. printk(KERN_NOTICE "%s: Not resetting disconnected device\n",
  1474. sc->name); /* P3 This floods. Remove soon. XXX */
  1475. } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
  1476. printk(KERN_NOTICE "%s: Not resetting multi-interface device\n",
  1477. sc->name); /* P3 This floods. Remove soon. XXX */
  1478. } else {
  1479. if ((lkr = usb_lock_device_for_reset(sc->dev, sc->intf)) < 0) {
  1480. printk(KERN_NOTICE
  1481. "%s: usb_lock_device_for_reset failed (%d)\n",
  1482. sc->name, lkr);
  1483. } else {
  1484. rc = usb_reset_device(sc->dev);
  1485. if (rc < 0) {
  1486. printk(KERN_NOTICE "%s: "
  1487. "usb_lock_device_for_reset failed (%d)\n",
  1488. sc->name, rc);
  1489. }
  1490. if (lkr)
  1491. usb_unlock_device(sc->dev);
  1492. }
  1493. }
  1494. /*
  1495. * In theory, no commands can be running while reset is active,
  1496. * so nobody can ask for another reset, and so we do not need any
  1497. * queues of resets or anything. We do need a spinlock though,
  1498. * to interact with block layer.
  1499. */
  1500. spin_lock_irqsave(&sc->lock, flags);
  1501. sc->reset = 0;
  1502. tasklet_schedule(&sc->tasklet);
  1503. list_for_each(p, &sc->luns) {
  1504. lun = list_entry(p, struct ub_lun, link);
  1505. blk_start_queue(lun->disk->queue);
  1506. }
  1507. wake_up(&sc->reset_wait);
  1508. spin_unlock_irqrestore(&sc->lock, flags);
  1509. }
  1510. /*
  1511. * This is called from a process context.
  1512. */
  1513. static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
  1514. {
  1515. lun->readonly = 0; /* XXX Query this from the device */
  1516. lun->capacity.nsec = 0;
  1517. lun->capacity.bsize = 512;
  1518. lun->capacity.bshift = 0;
  1519. if (ub_sync_tur(sc, lun) != 0)
  1520. return; /* Not ready */
  1521. lun->changed = 0;
  1522. if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
  1523. /*
  1524. * The retry here means something is wrong, either with the
  1525. * device, with the transport, or with our code.
  1526. * We keep this because sd.c has retries for capacity.
  1527. */
  1528. if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
  1529. lun->capacity.nsec = 0;
  1530. lun->capacity.bsize = 512;
  1531. lun->capacity.bshift = 0;
  1532. }
  1533. }
  1534. }
  1535. /*
  1536. * The open funcion.
  1537. * This is mostly needed to keep refcounting, but also to support
  1538. * media checks on removable media drives.
  1539. */
  1540. static int ub_bd_open(struct inode *inode, struct file *filp)
  1541. {
  1542. struct gendisk *disk = inode->i_bdev->bd_disk;
  1543. struct ub_lun *lun;
  1544. struct ub_dev *sc;
  1545. unsigned long flags;
  1546. int rc;
  1547. if ((lun = disk->private_data) == NULL)
  1548. return -ENXIO;
  1549. sc = lun->udev;
  1550. spin_lock_irqsave(&ub_lock, flags);
  1551. if (atomic_read(&sc->poison)) {
  1552. spin_unlock_irqrestore(&ub_lock, flags);
  1553. return -ENXIO;
  1554. }
  1555. sc->openc++;
  1556. spin_unlock_irqrestore(&ub_lock, flags);
  1557. /*
  1558. * This is a workaround for a specific problem in our block layer.
  1559. * In 2.6.9, register_disk duplicates the code from rescan_partitions.
  1560. * However, if we do add_disk with a device which persistently reports
  1561. * a changed media, add_disk calls register_disk, which does do_open,
  1562. * which will call rescan_paritions for changed media. After that,
  1563. * register_disk attempts to do it all again and causes double kobject
  1564. * registration and a eventually an oops on module removal.
  1565. *
  1566. * The bottom line is, Al Viro says that we should not allow
  1567. * bdev->bd_invalidated to be set when doing add_disk no matter what.
  1568. */
  1569. if (lun->first_open) {
  1570. lun->first_open = 0;
  1571. if (lun->changed) {
  1572. rc = -ENOMEDIUM;
  1573. goto err_open;
  1574. }
  1575. }
  1576. if (lun->removable || lun->readonly)
  1577. check_disk_change(inode->i_bdev);
  1578. /*
  1579. * The sd.c considers ->media_present and ->changed not equivalent,
  1580. * under some pretty murky conditions (a failure of READ CAPACITY).
  1581. * We may need it one day.
  1582. */
  1583. if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) {
  1584. rc = -ENOMEDIUM;
  1585. goto err_open;
  1586. }
  1587. if (lun->readonly && (filp->f_mode & FMODE_WRITE)) {
  1588. rc = -EROFS;
  1589. goto err_open;
  1590. }
  1591. return 0;
  1592. err_open:
  1593. ub_put(sc);
  1594. return rc;
  1595. }
  1596. /*
  1597. */
  1598. static int ub_bd_release(struct inode *inode, struct file *filp)
  1599. {
  1600. struct gendisk *disk = inode->i_bdev->bd_disk;
  1601. struct ub_lun *lun = disk->private_data;
  1602. struct ub_dev *sc = lun->udev;
  1603. ub_put(sc);
  1604. return 0;
  1605. }
  1606. /*
  1607. * The ioctl interface.
  1608. */
  1609. static int ub_bd_ioctl(struct inode *inode, struct file *filp,
  1610. unsigned int cmd, unsigned long arg)
  1611. {
  1612. struct gendisk *disk = inode->i_bdev->bd_disk;
  1613. void __user *usermem = (void __user *) arg;
  1614. return scsi_cmd_ioctl(filp, disk, cmd, usermem);
  1615. }
  1616. /*
  1617. * This is called once a new disk was seen by the block layer or by ub_probe().
  1618. * The main onjective here is to discover the features of the media such as
  1619. * the capacity, read-only status, etc. USB storage generally does not
  1620. * need to be spun up, but if we needed it, this would be the place.
  1621. *
  1622. * This call can sleep.
  1623. *
  1624. * The return code is not used.
  1625. */
  1626. static int ub_bd_revalidate(struct gendisk *disk)
  1627. {
  1628. struct ub_lun *lun = disk->private_data;
  1629. ub_revalidate(lun->udev, lun);
  1630. /* XXX Support sector size switching like in sr.c */
  1631. blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
  1632. set_capacity(disk, lun->capacity.nsec);
  1633. // set_disk_ro(sdkp->disk, lun->readonly);
  1634. return 0;
  1635. }
  1636. /*
  1637. * The check is called by the block layer to verify if the media
  1638. * is still available. It is supposed to be harmless, lightweight and
  1639. * non-intrusive in case the media was not changed.
  1640. *
  1641. * This call can sleep.
  1642. *
  1643. * The return code is bool!
  1644. */
  1645. static int ub_bd_media_changed(struct gendisk *disk)
  1646. {
  1647. struct ub_lun *lun = disk->private_data;
  1648. if (!lun->removable)
  1649. return 0;
  1650. /*
  1651. * We clean checks always after every command, so this is not
  1652. * as dangerous as it looks. If the TEST_UNIT_READY fails here,
  1653. * the device is actually not ready with operator or software
  1654. * intervention required. One dangerous item might be a drive which
  1655. * spins itself down, and come the time to write dirty pages, this
  1656. * will fail, then block layer discards the data. Since we never
  1657. * spin drives up, such devices simply cannot be used with ub anyway.
  1658. */
  1659. if (ub_sync_tur(lun->udev, lun) != 0) {
  1660. lun->changed = 1;
  1661. return 1;
  1662. }
  1663. return lun->changed;
  1664. }
  1665. static struct block_device_operations ub_bd_fops = {
  1666. .owner = THIS_MODULE,
  1667. .open = ub_bd_open,
  1668. .release = ub_bd_release,
  1669. .ioctl = ub_bd_ioctl,
  1670. .media_changed = ub_bd_media_changed,
  1671. .revalidate_disk = ub_bd_revalidate,
  1672. };
  1673. /*
  1674. * Common ->done routine for commands executed synchronously.
  1675. */
  1676. static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1677. {
  1678. struct completion *cop = cmd->back;
  1679. complete(cop);
  1680. }
  1681. /*
  1682. * Test if the device has a check condition on it, synchronously.
  1683. */
  1684. static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
  1685. {
  1686. struct ub_scsi_cmd *cmd;
  1687. enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
  1688. unsigned long flags;
  1689. struct completion compl;
  1690. int rc;
  1691. init_completion(&compl);
  1692. rc = -ENOMEM;
  1693. if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
  1694. goto err_alloc;
  1695. memset(cmd, 0, ALLOC_SIZE);
  1696. cmd->cdb[0] = TEST_UNIT_READY;
  1697. cmd->cdb_len = 6;
  1698. cmd->dir = UB_DIR_NONE;
  1699. cmd->state = UB_CMDST_INIT;
  1700. cmd->lun = lun; /* This may be NULL, but that's ok */
  1701. cmd->done = ub_probe_done;
  1702. cmd->back = &compl;
  1703. spin_lock_irqsave(&sc->lock, flags);
  1704. cmd->tag = sc->tagcnt++;
  1705. rc = ub_submit_scsi(sc, cmd);
  1706. spin_unlock_irqrestore(&sc->lock, flags);
  1707. if (rc != 0) {
  1708. printk("ub: testing ready: submit error (%d)\n", rc); /* P3 */
  1709. goto err_submit;
  1710. }
  1711. wait_for_completion(&compl);
  1712. rc = cmd->error;
  1713. if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
  1714. rc = cmd->key;
  1715. err_submit:
  1716. kfree(cmd);
  1717. err_alloc:
  1718. return rc;
  1719. }
  1720. /*
  1721. * Read the SCSI capacity synchronously (for probing).
  1722. */
  1723. static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
  1724. struct ub_capacity *ret)
  1725. {
  1726. struct ub_scsi_cmd *cmd;
  1727. struct scatterlist *sg;
  1728. char *p;
  1729. enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
  1730. unsigned long flags;
  1731. unsigned int bsize, shift;
  1732. unsigned long nsec;
  1733. struct completion compl;
  1734. int rc;
  1735. init_completion(&compl);
  1736. rc = -ENOMEM;
  1737. if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
  1738. goto err_alloc;
  1739. memset(cmd, 0, ALLOC_SIZE);
  1740. p = (char *)cmd + sizeof(struct ub_scsi_cmd);
  1741. cmd->cdb[0] = 0x25;
  1742. cmd->cdb_len = 10;
  1743. cmd->dir = UB_DIR_READ;
  1744. cmd->state = UB_CMDST_INIT;
  1745. cmd->nsg = 1;
  1746. sg = &cmd->sgv[0];
  1747. sg->page = virt_to_page(p);
  1748. sg->offset = (unsigned long)p & (PAGE_SIZE-1);
  1749. sg->length = 8;
  1750. cmd->len = 8;
  1751. cmd->lun = lun;
  1752. cmd->done = ub_probe_done;
  1753. cmd->back = &compl;
  1754. spin_lock_irqsave(&sc->lock, flags);
  1755. cmd->tag = sc->tagcnt++;
  1756. rc = ub_submit_scsi(sc, cmd);
  1757. spin_unlock_irqrestore(&sc->lock, flags);
  1758. if (rc != 0) {
  1759. printk("ub: reading capacity: submit error (%d)\n", rc); /* P3 */
  1760. goto err_submit;
  1761. }
  1762. wait_for_completion(&compl);
  1763. if (cmd->error != 0) {
  1764. printk("ub: reading capacity: error %d\n", cmd->error); /* P3 */
  1765. rc = -EIO;
  1766. goto err_read;
  1767. }
  1768. if (cmd->act_len != 8) {
  1769. printk("ub: reading capacity: size %d\n", cmd->act_len); /* P3 */
  1770. rc = -EIO;
  1771. goto err_read;
  1772. }
  1773. /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
  1774. nsec = be32_to_cpu(*(__be32 *)p) + 1;
  1775. bsize = be32_to_cpu(*(__be32 *)(p + 4));
  1776. switch (bsize) {
  1777. case 512: shift = 0; break;
  1778. case 1024: shift = 1; break;
  1779. case 2048: shift = 2; break;
  1780. case 4096: shift = 3; break;
  1781. default:
  1782. printk("ub: Bad sector size %u\n", bsize); /* P3 */
  1783. rc = -EDOM;
  1784. goto err_inv_bsize;
  1785. }
  1786. ret->bsize = bsize;
  1787. ret->bshift = shift;
  1788. ret->nsec = nsec << shift;
  1789. rc = 0;
  1790. err_inv_bsize:
  1791. err_read:
  1792. err_submit:
  1793. kfree(cmd);
  1794. err_alloc:
  1795. return rc;
  1796. }
  1797. /*
  1798. */
  1799. static void ub_probe_urb_complete(struct urb *urb, struct pt_regs *pt)
  1800. {
  1801. struct completion *cop = urb->context;
  1802. complete(cop);
  1803. }
  1804. static void ub_probe_timeout(unsigned long arg)
  1805. {
  1806. struct completion *cop = (struct completion *) arg;
  1807. complete(cop);
  1808. }
  1809. /*
  1810. * Get number of LUNs by the way of Bulk GetMaxLUN command.
  1811. */
  1812. static int ub_sync_getmaxlun(struct ub_dev *sc)
  1813. {
  1814. int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
  1815. unsigned char *p;
  1816. enum { ALLOC_SIZE = 1 };
  1817. struct usb_ctrlrequest *cr;
  1818. struct completion compl;
  1819. struct timer_list timer;
  1820. int nluns;
  1821. int rc;
  1822. init_completion(&compl);
  1823. rc = -ENOMEM;
  1824. if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
  1825. goto err_alloc;
  1826. *p = 55;
  1827. cr = &sc->work_cr;
  1828. cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  1829. cr->bRequest = US_BULK_GET_MAX_LUN;
  1830. cr->wValue = cpu_to_le16(0);
  1831. cr->wIndex = cpu_to_le16(ifnum);
  1832. cr->wLength = cpu_to_le16(1);
  1833. usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
  1834. (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
  1835. sc->work_urb.actual_length = 0;
  1836. sc->work_urb.error_count = 0;
  1837. sc->work_urb.status = 0;
  1838. if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
  1839. if (rc == -EPIPE) {
  1840. printk("%s: Stall submitting GetMaxLUN, using 1 LUN\n",
  1841. sc->name); /* P3 */
  1842. } else {
  1843. printk(KERN_NOTICE
  1844. "%s: Unable to submit GetMaxLUN (%d)\n",
  1845. sc->name, rc);
  1846. }
  1847. goto err_submit;
  1848. }
  1849. init_timer(&timer);
  1850. timer.function = ub_probe_timeout;
  1851. timer.data = (unsigned long) &compl;
  1852. timer.expires = jiffies + UB_CTRL_TIMEOUT;
  1853. add_timer(&timer);
  1854. wait_for_completion(&compl);
  1855. del_timer_sync(&timer);
  1856. usb_kill_urb(&sc->work_urb);
  1857. if ((rc = sc->work_urb.status) < 0) {
  1858. if (rc == -EPIPE) {
  1859. printk("%s: Stall at GetMaxLUN, using 1 LUN\n",
  1860. sc->name); /* P3 */
  1861. } else {
  1862. printk(KERN_NOTICE
  1863. "%s: Error at GetMaxLUN (%d)\n",
  1864. sc->name, rc);
  1865. }
  1866. goto err_io;
  1867. }
  1868. if (sc->work_urb.actual_length != 1) {
  1869. printk("%s: GetMaxLUN returned %d bytes\n", sc->name,
  1870. sc->work_urb.actual_length); /* P3 */
  1871. nluns = 0;
  1872. } else {
  1873. if ((nluns = *p) == 55) {
  1874. nluns = 0;
  1875. } else {
  1876. /* GetMaxLUN returns the maximum LUN number */
  1877. nluns += 1;
  1878. if (nluns > UB_MAX_LUNS)
  1879. nluns = UB_MAX_LUNS;
  1880. }
  1881. printk("%s: GetMaxLUN returned %d, using %d LUNs\n", sc->name,
  1882. *p, nluns); /* P3 */
  1883. }
  1884. kfree(p);
  1885. return nluns;
  1886. err_io:
  1887. err_submit:
  1888. kfree(p);
  1889. err_alloc:
  1890. return rc;
  1891. }
  1892. /*
  1893. * Clear initial stalls.
  1894. */
  1895. static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
  1896. {
  1897. int endp;
  1898. struct usb_ctrlrequest *cr;
  1899. struct completion compl;
  1900. struct timer_list timer;
  1901. int rc;
  1902. init_completion(&compl);
  1903. endp = usb_pipeendpoint(stalled_pipe);
  1904. if (usb_pipein (stalled_pipe))
  1905. endp |= USB_DIR_IN;
  1906. cr = &sc->work_cr;
  1907. cr->bRequestType = USB_RECIP_ENDPOINT;
  1908. cr->bRequest = USB_REQ_CLEAR_FEATURE;
  1909. cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
  1910. cr->wIndex = cpu_to_le16(endp);
  1911. cr->wLength = cpu_to_le16(0);
  1912. usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
  1913. (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
  1914. sc->work_urb.actual_length = 0;
  1915. sc->work_urb.error_count = 0;
  1916. sc->work_urb.status = 0;
  1917. if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
  1918. printk(KERN_WARNING
  1919. "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
  1920. return rc;
  1921. }
  1922. init_timer(&timer);
  1923. timer.function = ub_probe_timeout;
  1924. timer.data = (unsigned long) &compl;
  1925. timer.expires = jiffies + UB_CTRL_TIMEOUT;
  1926. add_timer(&timer);
  1927. wait_for_completion(&compl);
  1928. del_timer_sync(&timer);
  1929. usb_kill_urb(&sc->work_urb);
  1930. /* reset the endpoint toggle */
  1931. usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
  1932. return 0;
  1933. }
  1934. /*
  1935. * Get the pipe settings.
  1936. */
  1937. static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
  1938. struct usb_interface *intf)
  1939. {
  1940. struct usb_host_interface *altsetting = intf->cur_altsetting;
  1941. struct usb_endpoint_descriptor *ep_in = NULL;
  1942. struct usb_endpoint_descriptor *ep_out = NULL;
  1943. struct usb_endpoint_descriptor *ep;
  1944. int i;
  1945. /*
  1946. * Find the endpoints we need.
  1947. * We are expecting a minimum of 2 endpoints - in and out (bulk).
  1948. * We will ignore any others.
  1949. */
  1950. for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
  1951. ep = &altsetting->endpoint[i].desc;
  1952. /* Is it a BULK endpoint? */
  1953. if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  1954. == USB_ENDPOINT_XFER_BULK) {
  1955. /* BULK in or out? */
  1956. if (ep->bEndpointAddress & USB_DIR_IN)
  1957. ep_in = ep;
  1958. else
  1959. ep_out = ep;
  1960. }
  1961. }
  1962. if (ep_in == NULL || ep_out == NULL) {
  1963. printk(KERN_NOTICE "%s: failed endpoint check\n",
  1964. sc->name);
  1965. return -ENODEV;
  1966. }
  1967. /* Calculate and store the pipe values */
  1968. sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
  1969. sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
  1970. sc->send_bulk_pipe = usb_sndbulkpipe(dev,
  1971. ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  1972. sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
  1973. ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  1974. return 0;
  1975. }
  1976. /*
  1977. * Probing is done in the process context, which allows us to cheat
  1978. * and not to build a state machine for the discovery.
  1979. */
  1980. static int ub_probe(struct usb_interface *intf,
  1981. const struct usb_device_id *dev_id)
  1982. {
  1983. struct ub_dev *sc;
  1984. int nluns;
  1985. int rc;
  1986. int i;
  1987. if (usb_usual_check_type(dev_id, USB_US_TYPE_UB))
  1988. return -ENXIO;
  1989. rc = -ENOMEM;
  1990. if ((sc = kmalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
  1991. goto err_core;
  1992. memset(sc, 0, sizeof(struct ub_dev));
  1993. spin_lock_init(&sc->lock);
  1994. INIT_LIST_HEAD(&sc->luns);
  1995. usb_init_urb(&sc->work_urb);
  1996. tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
  1997. atomic_set(&sc->poison, 0);
  1998. INIT_WORK(&sc->reset_work, ub_reset_task, sc);
  1999. init_waitqueue_head(&sc->reset_wait);
  2000. init_timer(&sc->work_timer);
  2001. sc->work_timer.data = (unsigned long) sc;
  2002. sc->work_timer.function = ub_urb_timeout;
  2003. ub_init_completion(&sc->work_done);
  2004. sc->work_done.done = 1; /* A little yuk, but oh well... */
  2005. sc->dev = interface_to_usbdev(intf);
  2006. sc->intf = intf;
  2007. // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  2008. usb_set_intfdata(intf, sc);
  2009. usb_get_dev(sc->dev);
  2010. // usb_get_intf(sc->intf); /* Do we need this? */
  2011. snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
  2012. sc->dev->bus->busnum, sc->dev->devnum);
  2013. /* XXX Verify that we can handle the device (from descriptors) */
  2014. if (ub_get_pipes(sc, sc->dev, intf) != 0)
  2015. goto err_dev_desc;
  2016. if (device_create_file(&sc->intf->dev, &dev_attr_diag) != 0)
  2017. goto err_diag;
  2018. /*
  2019. * At this point, all USB initialization is done, do upper layer.
  2020. * We really hate halfway initialized structures, so from the
  2021. * invariants perspective, this ub_dev is fully constructed at
  2022. * this point.
  2023. */
  2024. /*
  2025. * This is needed to clear toggles. It is a problem only if we do
  2026. * `rmmod ub && modprobe ub` without disconnects, but we like that.
  2027. */
  2028. #if 0 /* iPod Mini fails if we do this (big white iPod works) */
  2029. ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
  2030. ub_probe_clear_stall(sc, sc->send_bulk_pipe);
  2031. #endif
  2032. /*
  2033. * The way this is used by the startup code is a little specific.
  2034. * A SCSI check causes a USB stall. Our common case code sees it
  2035. * and clears the check, after which the device is ready for use.
  2036. * But if a check was not present, any command other than
  2037. * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
  2038. *
  2039. * If we neglect to clear the SCSI check, the first real command fails
  2040. * (which is the capacity readout). We clear that and retry, but why
  2041. * causing spurious retries for no reason.
  2042. *
  2043. * Revalidation may start with its own TEST_UNIT_READY, but that one
  2044. * has to succeed, so we clear checks with an additional one here.
  2045. * In any case it's not our business how revaliadation is implemented.
  2046. */
  2047. for (i = 0; i < 3; i++) { /* Retries for benh's key */
  2048. if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
  2049. if (rc != 0x6) break;
  2050. msleep(10);
  2051. }
  2052. nluns = 1;
  2053. for (i = 0; i < 3; i++) {
  2054. if ((rc = ub_sync_getmaxlun(sc)) < 0) {
  2055. /*
  2056. * This segment is taken from usb-storage. They say
  2057. * that ZIP-100 needs this, but my own ZIP-100 works
  2058. * fine without this.
  2059. * Still, it does not seem to hurt anything.
  2060. */
  2061. if (rc == -EPIPE) {
  2062. ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
  2063. ub_probe_clear_stall(sc, sc->send_bulk_pipe);
  2064. }
  2065. break;
  2066. }
  2067. if (rc != 0) {
  2068. nluns = rc;
  2069. break;
  2070. }
  2071. msleep(100);
  2072. }
  2073. for (i = 0; i < nluns; i++) {
  2074. ub_probe_lun(sc, i);
  2075. }
  2076. return 0;
  2077. /* device_remove_file(&sc->intf->dev, &dev_attr_diag); */
  2078. err_diag:
  2079. err_dev_desc:
  2080. usb_set_intfdata(intf, NULL);
  2081. // usb_put_intf(sc->intf);
  2082. usb_put_dev(sc->dev);
  2083. kfree(sc);
  2084. err_core:
  2085. return rc;
  2086. }
  2087. static int ub_probe_lun(struct ub_dev *sc, int lnum)
  2088. {
  2089. struct ub_lun *lun;
  2090. request_queue_t *q;
  2091. struct gendisk *disk;
  2092. int rc;
  2093. rc = -ENOMEM;
  2094. if ((lun = kmalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
  2095. goto err_alloc;
  2096. memset(lun, 0, sizeof(struct ub_lun));
  2097. lun->num = lnum;
  2098. rc = -ENOSR;
  2099. if ((lun->id = ub_id_get()) == -1)
  2100. goto err_id;
  2101. lun->udev = sc;
  2102. list_add(&lun->link, &sc->luns);
  2103. snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
  2104. lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
  2105. lun->removable = 1; /* XXX Query this from the device */
  2106. lun->changed = 1; /* ub_revalidate clears only */
  2107. lun->first_open = 1;
  2108. ub_revalidate(sc, lun);
  2109. rc = -ENOMEM;
  2110. if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
  2111. goto err_diskalloc;
  2112. lun->disk = disk;
  2113. sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
  2114. sprintf(disk->devfs_name, DEVFS_NAME "/%c", lun->id + 'a');
  2115. disk->major = UB_MAJOR;
  2116. disk->first_minor = lun->id * UB_PARTS_PER_LUN;
  2117. disk->fops = &ub_bd_fops;
  2118. disk->private_data = lun;
  2119. disk->driverfs_dev = &sc->intf->dev;
  2120. rc = -ENOMEM;
  2121. if ((q = blk_init_queue(ub_request_fn, &sc->lock)) == NULL)
  2122. goto err_blkqinit;
  2123. disk->queue = q;
  2124. blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
  2125. blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
  2126. blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
  2127. blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
  2128. blk_queue_max_sectors(q, UB_MAX_SECTORS);
  2129. blk_queue_hardsect_size(q, lun->capacity.bsize);
  2130. q->queuedata = lun;
  2131. set_capacity(disk, lun->capacity.nsec);
  2132. if (lun->removable)
  2133. disk->flags |= GENHD_FL_REMOVABLE;
  2134. add_disk(disk);
  2135. return 0;
  2136. err_blkqinit:
  2137. put_disk(disk);
  2138. err_diskalloc:
  2139. list_del(&lun->link);
  2140. ub_id_put(lun->id);
  2141. err_id:
  2142. kfree(lun);
  2143. err_alloc:
  2144. return rc;
  2145. }
  2146. static void ub_disconnect(struct usb_interface *intf)
  2147. {
  2148. struct ub_dev *sc = usb_get_intfdata(intf);
  2149. struct list_head *p;
  2150. struct ub_lun *lun;
  2151. struct gendisk *disk;
  2152. unsigned long flags;
  2153. /*
  2154. * Prevent ub_bd_release from pulling the rug from under us.
  2155. * XXX This is starting to look like a kref.
  2156. * XXX Why not to take this ref at probe time?
  2157. */
  2158. spin_lock_irqsave(&ub_lock, flags);
  2159. sc->openc++;
  2160. spin_unlock_irqrestore(&ub_lock, flags);
  2161. /*
  2162. * Fence stall clearnings, operations triggered by unlinkings and so on.
  2163. * We do not attempt to unlink any URBs, because we do not trust the
  2164. * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
  2165. */
  2166. atomic_set(&sc->poison, 1);
  2167. /*
  2168. * Wait for reset to end, if any.
  2169. */
  2170. wait_event(sc->reset_wait, !sc->reset);
  2171. /*
  2172. * Blow away queued commands.
  2173. *
  2174. * Actually, this never works, because before we get here
  2175. * the HCD terminates outstanding URB(s). It causes our
  2176. * SCSI command queue to advance, commands fail to submit,
  2177. * and the whole queue drains. So, we just use this code to
  2178. * print warnings.
  2179. */
  2180. spin_lock_irqsave(&sc->lock, flags);
  2181. {
  2182. struct ub_scsi_cmd *cmd;
  2183. int cnt = 0;
  2184. while ((cmd = ub_cmdq_peek(sc)) != NULL) {
  2185. cmd->error = -ENOTCONN;
  2186. cmd->state = UB_CMDST_DONE;
  2187. ub_cmdtr_state(sc, cmd);
  2188. ub_cmdq_pop(sc);
  2189. (*cmd->done)(sc, cmd);
  2190. cnt++;
  2191. }
  2192. if (cnt != 0) {
  2193. printk(KERN_WARNING "%s: "
  2194. "%d was queued after shutdown\n", sc->name, cnt);
  2195. }
  2196. }
  2197. spin_unlock_irqrestore(&sc->lock, flags);
  2198. /*
  2199. * Unregister the upper layer.
  2200. */
  2201. list_for_each (p, &sc->luns) {
  2202. lun = list_entry(p, struct ub_lun, link);
  2203. disk = lun->disk;
  2204. if (disk->flags & GENHD_FL_UP)
  2205. del_gendisk(disk);
  2206. /*
  2207. * I wish I could do:
  2208. * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
  2209. * As it is, we rely on our internal poisoning and let
  2210. * the upper levels to spin furiously failing all the I/O.
  2211. */
  2212. }
  2213. /*
  2214. * Taking a lock on a structure which is about to be freed
  2215. * is very nonsensual. Here it is largely a way to do a debug freeze,
  2216. * and a bracket which shows where the nonsensual code segment ends.
  2217. *
  2218. * Testing for -EINPROGRESS is always a bug, so we are bending
  2219. * the rules a little.
  2220. */
  2221. spin_lock_irqsave(&sc->lock, flags);
  2222. if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
  2223. printk(KERN_WARNING "%s: "
  2224. "URB is active after disconnect\n", sc->name);
  2225. }
  2226. spin_unlock_irqrestore(&sc->lock, flags);
  2227. /*
  2228. * There is virtually no chance that other CPU runs times so long
  2229. * after ub_urb_complete should have called del_timer, but only if HCD
  2230. * didn't forget to deliver a callback on unlink.
  2231. */
  2232. del_timer_sync(&sc->work_timer);
  2233. /*
  2234. * At this point there must be no commands coming from anyone
  2235. * and no URBs left in transit.
  2236. */
  2237. device_remove_file(&sc->intf->dev, &dev_attr_diag);
  2238. usb_set_intfdata(intf, NULL);
  2239. // usb_put_intf(sc->intf);
  2240. sc->intf = NULL;
  2241. usb_put_dev(sc->dev);
  2242. sc->dev = NULL;
  2243. ub_put(sc);
  2244. }
  2245. static struct usb_driver ub_driver = {
  2246. .name = "ub",
  2247. .probe = ub_probe,
  2248. .disconnect = ub_disconnect,
  2249. .id_table = ub_usb_ids,
  2250. };
  2251. static int __init ub_init(void)
  2252. {
  2253. int rc;
  2254. if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
  2255. goto err_regblkdev;
  2256. devfs_mk_dir(DEVFS_NAME);
  2257. if ((rc = usb_register(&ub_driver)) != 0)
  2258. goto err_register;
  2259. usb_usual_set_present(USB_US_TYPE_UB);
  2260. return 0;
  2261. err_register:
  2262. devfs_remove(DEVFS_NAME);
  2263. unregister_blkdev(UB_MAJOR, DRV_NAME);
  2264. err_regblkdev:
  2265. return rc;
  2266. }
  2267. static void __exit ub_exit(void)
  2268. {
  2269. usb_deregister(&ub_driver);
  2270. devfs_remove(DEVFS_NAME);
  2271. unregister_blkdev(UB_MAJOR, DRV_NAME);
  2272. usb_usual_clear_present(USB_US_TYPE_UB);
  2273. }
  2274. module_init(ub_init);
  2275. module_exit(ub_exit);
  2276. MODULE_LICENSE("GPL");