ub.c 61 KB

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