ub.c 63 KB

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