ub.c 63 KB

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