ub.c 63 KB

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