ub.c 62 KB

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