ub.c 57 KB

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