ub.c 62 KB

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