ub.c 62 KB

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