ub.c 63 KB

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