ub.c 64 KB

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