ub.c 62 KB

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