ub.c 63 KB

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