ub.c 68 KB

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