ub.c 69 KB

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