ub.c 64 KB

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