ub.c 62 KB

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