ub.c 62 KB

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