ub.c 62 KB

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