ub.c 67 KB

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