bas-gigaset.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  1. /*
  2. * USB driver for Gigaset 307x base via direct USB connection.
  3. *
  4. * Copyright (c) 2001 by Hansjoerg Lipp <hjlipp@web.de>,
  5. * Tilman Schmidt <tilman@imap.cc>,
  6. * Stefan Eilers.
  7. *
  8. * =====================================================================
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. * =====================================================================
  14. */
  15. #include "gigaset.h"
  16. #include <linux/usb.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. /* Version Information */
  20. #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
  21. #define DRIVER_DESC "USB Driver for Gigaset 307x"
  22. /* Module parameters */
  23. static int startmode = SM_ISDN;
  24. static int cidmode = 1;
  25. module_param(startmode, int, S_IRUGO);
  26. module_param(cidmode, int, S_IRUGO);
  27. MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
  28. MODULE_PARM_DESC(cidmode, "Call-ID mode");
  29. #define GIGASET_MINORS 1
  30. #define GIGASET_MINOR 16
  31. #define GIGASET_MODULENAME "bas_gigaset"
  32. #define GIGASET_DEVNAME "ttyGB"
  33. /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
  34. #define IF_WRITEBUF 264
  35. /* interrupt pipe message size according to ibid. ch. 2.2 */
  36. #define IP_MSGSIZE 3
  37. /* Values for the Gigaset 307x */
  38. #define USB_GIGA_VENDOR_ID 0x0681
  39. #define USB_3070_PRODUCT_ID 0x0001
  40. #define USB_3075_PRODUCT_ID 0x0002
  41. #define USB_SX303_PRODUCT_ID 0x0021
  42. #define USB_SX353_PRODUCT_ID 0x0022
  43. /* table of devices that work with this driver */
  44. static const struct usb_device_id gigaset_table[] = {
  45. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
  46. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
  47. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
  48. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
  49. { } /* Terminating entry */
  50. };
  51. MODULE_DEVICE_TABLE(usb, gigaset_table);
  52. /*======================= local function prototypes ==========================*/
  53. /* function called if a new device belonging to this driver is connected */
  54. static int gigaset_probe(struct usb_interface *interface,
  55. const struct usb_device_id *id);
  56. /* Function will be called if the device is unplugged */
  57. static void gigaset_disconnect(struct usb_interface *interface);
  58. /* functions called before/after suspend */
  59. static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
  60. static int gigaset_resume(struct usb_interface *intf);
  61. /* functions called before/after device reset */
  62. static int gigaset_pre_reset(struct usb_interface *intf);
  63. static int gigaset_post_reset(struct usb_interface *intf);
  64. static int atread_submit(struct cardstate *, int);
  65. static void stopurbs(struct bas_bc_state *);
  66. static int req_submit(struct bc_state *, int, int, int);
  67. static int atwrite_submit(struct cardstate *, unsigned char *, int);
  68. static int start_cbsend(struct cardstate *);
  69. /*============================================================================*/
  70. struct bas_cardstate {
  71. struct usb_device *udev; /* USB device pointer */
  72. struct usb_interface *interface; /* interface for this device */
  73. unsigned char minor; /* starting minor number */
  74. struct urb *urb_ctrl; /* control pipe default URB */
  75. struct usb_ctrlrequest dr_ctrl;
  76. struct timer_list timer_ctrl; /* control request timeout */
  77. int retry_ctrl;
  78. struct timer_list timer_atrdy; /* AT command ready timeout */
  79. struct urb *urb_cmd_out; /* for sending AT commands */
  80. struct usb_ctrlrequest dr_cmd_out;
  81. int retry_cmd_out;
  82. struct urb *urb_cmd_in; /* for receiving AT replies */
  83. struct usb_ctrlrequest dr_cmd_in;
  84. struct timer_list timer_cmd_in; /* receive request timeout */
  85. unsigned char *rcvbuf; /* AT reply receive buffer */
  86. struct urb *urb_int_in; /* URB for interrupt pipe */
  87. unsigned char *int_in_buf;
  88. spinlock_t lock; /* locks all following */
  89. int basstate; /* bitmap (BS_*) */
  90. int pending; /* uncompleted base request */
  91. wait_queue_head_t waitqueue;
  92. int rcvbuf_size; /* size of AT receive buffer */
  93. /* 0: no receive in progress */
  94. int retry_cmd_in; /* receive req retry count */
  95. };
  96. /* status of direct USB connection to 307x base (bits in basstate) */
  97. #define BS_ATOPEN 0x001 /* AT channel open */
  98. #define BS_B1OPEN 0x002 /* B channel 1 open */
  99. #define BS_B2OPEN 0x004 /* B channel 2 open */
  100. #define BS_ATREADY 0x008 /* base ready for AT command */
  101. #define BS_INIT 0x010 /* base has signalled INIT_OK */
  102. #define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
  103. #define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
  104. #define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
  105. #define BS_SUSPEND 0x100 /* USB port suspended */
  106. #define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
  107. static struct gigaset_driver *driver;
  108. /* usb specific object needed to register this driver with the usb subsystem */
  109. static struct usb_driver gigaset_usb_driver = {
  110. .name = GIGASET_MODULENAME,
  111. .probe = gigaset_probe,
  112. .disconnect = gigaset_disconnect,
  113. .id_table = gigaset_table,
  114. .suspend = gigaset_suspend,
  115. .resume = gigaset_resume,
  116. .reset_resume = gigaset_post_reset,
  117. .pre_reset = gigaset_pre_reset,
  118. .post_reset = gigaset_post_reset,
  119. };
  120. /* get message text for usb_submit_urb return code
  121. */
  122. static char *get_usb_rcmsg(int rc)
  123. {
  124. static char unkmsg[28];
  125. switch (rc) {
  126. case 0:
  127. return "success";
  128. case -ENOMEM:
  129. return "out of memory";
  130. case -ENODEV:
  131. return "device not present";
  132. case -ENOENT:
  133. return "endpoint not present";
  134. case -ENXIO:
  135. return "URB type not supported";
  136. case -EINVAL:
  137. return "invalid argument";
  138. case -EAGAIN:
  139. return "start frame too early or too much scheduled";
  140. case -EFBIG:
  141. return "too many isochronous frames requested";
  142. case -EPIPE:
  143. return "endpoint stalled";
  144. case -EMSGSIZE:
  145. return "invalid packet size";
  146. case -ENOSPC:
  147. return "would overcommit USB bandwidth";
  148. case -ESHUTDOWN:
  149. return "device shut down";
  150. case -EPERM:
  151. return "reject flag set";
  152. case -EHOSTUNREACH:
  153. return "device suspended";
  154. default:
  155. snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
  156. return unkmsg;
  157. }
  158. }
  159. /* get message text for USB status code
  160. */
  161. static char *get_usb_statmsg(int status)
  162. {
  163. static char unkmsg[28];
  164. switch (status) {
  165. case 0:
  166. return "success";
  167. case -ENOENT:
  168. return "unlinked (sync)";
  169. case -EINPROGRESS:
  170. return "pending";
  171. case -EPROTO:
  172. return "bit stuffing error, timeout, or unknown USB error";
  173. case -EILSEQ:
  174. return "CRC mismatch, timeout, or unknown USB error";
  175. case -ETIME:
  176. return "timed out";
  177. case -EPIPE:
  178. return "endpoint stalled";
  179. case -ECOMM:
  180. return "IN buffer overrun";
  181. case -ENOSR:
  182. return "OUT buffer underrun";
  183. case -EOVERFLOW:
  184. return "too much data";
  185. case -EREMOTEIO:
  186. return "short packet detected";
  187. case -ENODEV:
  188. return "device removed";
  189. case -EXDEV:
  190. return "partial isochronous transfer";
  191. case -EINVAL:
  192. return "invalid argument";
  193. case -ECONNRESET:
  194. return "unlinked (async)";
  195. case -ESHUTDOWN:
  196. return "device shut down";
  197. default:
  198. snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
  199. return unkmsg;
  200. }
  201. }
  202. /* usb_pipetype_str
  203. * retrieve string representation of USB pipe type
  204. */
  205. static inline char *usb_pipetype_str(int pipe)
  206. {
  207. if (usb_pipeisoc(pipe))
  208. return "Isoc";
  209. if (usb_pipeint(pipe))
  210. return "Int";
  211. if (usb_pipecontrol(pipe))
  212. return "Ctrl";
  213. if (usb_pipebulk(pipe))
  214. return "Bulk";
  215. return "?";
  216. }
  217. /* dump_urb
  218. * write content of URB to syslog for debugging
  219. */
  220. static inline void dump_urb(enum debuglevel level, const char *tag,
  221. struct urb *urb)
  222. {
  223. #ifdef CONFIG_GIGASET_DEBUG
  224. int i;
  225. gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
  226. if (urb) {
  227. gig_dbg(level,
  228. " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
  229. "hcpriv=0x%08lx, transfer_flags=0x%x,",
  230. (unsigned long) urb->dev,
  231. usb_pipetype_str(urb->pipe),
  232. usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
  233. usb_pipein(urb->pipe) ? "in" : "out",
  234. (unsigned long) urb->hcpriv,
  235. urb->transfer_flags);
  236. gig_dbg(level,
  237. " transfer_buffer=0x%08lx[%d], actual_length=%d, "
  238. "setup_packet=0x%08lx,",
  239. (unsigned long) urb->transfer_buffer,
  240. urb->transfer_buffer_length, urb->actual_length,
  241. (unsigned long) urb->setup_packet);
  242. gig_dbg(level,
  243. " start_frame=%d, number_of_packets=%d, interval=%d, "
  244. "error_count=%d,",
  245. urb->start_frame, urb->number_of_packets, urb->interval,
  246. urb->error_count);
  247. gig_dbg(level,
  248. " context=0x%08lx, complete=0x%08lx, "
  249. "iso_frame_desc[]={",
  250. (unsigned long) urb->context,
  251. (unsigned long) urb->complete);
  252. for (i = 0; i < urb->number_of_packets; i++) {
  253. struct usb_iso_packet_descriptor *pifd
  254. = &urb->iso_frame_desc[i];
  255. gig_dbg(level,
  256. " {offset=%u, length=%u, actual_length=%u, "
  257. "status=%u}",
  258. pifd->offset, pifd->length, pifd->actual_length,
  259. pifd->status);
  260. }
  261. }
  262. gig_dbg(level, "}}");
  263. #endif
  264. }
  265. /* read/set modem control bits etc. (m10x only) */
  266. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  267. unsigned new_state)
  268. {
  269. return -EINVAL;
  270. }
  271. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  272. {
  273. return -EINVAL;
  274. }
  275. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  276. {
  277. return -EINVAL;
  278. }
  279. /* set/clear bits in base connection state, return previous state
  280. */
  281. static inline int update_basstate(struct bas_cardstate *ucs,
  282. int set, int clear)
  283. {
  284. unsigned long flags;
  285. int state;
  286. spin_lock_irqsave(&ucs->lock, flags);
  287. state = ucs->basstate;
  288. ucs->basstate = (state & ~clear) | set;
  289. spin_unlock_irqrestore(&ucs->lock, flags);
  290. return state;
  291. }
  292. /* error_hangup
  293. * hang up any existing connection because of an unrecoverable error
  294. * This function may be called from any context and takes care of scheduling
  295. * the necessary actions for execution outside of interrupt context.
  296. * cs->lock must not be held.
  297. * argument:
  298. * B channel control structure
  299. */
  300. static inline void error_hangup(struct bc_state *bcs)
  301. {
  302. struct cardstate *cs = bcs->cs;
  303. gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
  304. gigaset_schedule_event(cs);
  305. }
  306. /* error_reset
  307. * reset Gigaset device because of an unrecoverable error
  308. * This function may be called from any context, and takes care of
  309. * scheduling the necessary actions for execution outside of interrupt context.
  310. * cs->hw.bas->lock must not be held.
  311. * argument:
  312. * controller state structure
  313. */
  314. static inline void error_reset(struct cardstate *cs)
  315. {
  316. /* reset interrupt pipe to recover (ignore errors) */
  317. update_basstate(cs->hw.bas, BS_RESETTING, 0);
  318. if (req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT))
  319. /* submission failed, escalate to USB port reset */
  320. usb_queue_reset_device(cs->hw.bas->interface);
  321. }
  322. /* check_pending
  323. * check for completion of pending control request
  324. * parameter:
  325. * ucs hardware specific controller state structure
  326. */
  327. static void check_pending(struct bas_cardstate *ucs)
  328. {
  329. unsigned long flags;
  330. spin_lock_irqsave(&ucs->lock, flags);
  331. switch (ucs->pending) {
  332. case 0:
  333. break;
  334. case HD_OPEN_ATCHANNEL:
  335. if (ucs->basstate & BS_ATOPEN)
  336. ucs->pending = 0;
  337. break;
  338. case HD_OPEN_B1CHANNEL:
  339. if (ucs->basstate & BS_B1OPEN)
  340. ucs->pending = 0;
  341. break;
  342. case HD_OPEN_B2CHANNEL:
  343. if (ucs->basstate & BS_B2OPEN)
  344. ucs->pending = 0;
  345. break;
  346. case HD_CLOSE_ATCHANNEL:
  347. if (!(ucs->basstate & BS_ATOPEN))
  348. ucs->pending = 0;
  349. break;
  350. case HD_CLOSE_B1CHANNEL:
  351. if (!(ucs->basstate & BS_B1OPEN))
  352. ucs->pending = 0;
  353. break;
  354. case HD_CLOSE_B2CHANNEL:
  355. if (!(ucs->basstate & BS_B2OPEN))
  356. ucs->pending = 0;
  357. break;
  358. case HD_DEVICE_INIT_ACK: /* no reply expected */
  359. ucs->pending = 0;
  360. break;
  361. case HD_RESET_INTERRUPT_PIPE:
  362. if (!(ucs->basstate & BS_RESETTING))
  363. ucs->pending = 0;
  364. break;
  365. /*
  366. * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
  367. * and should never end up here
  368. */
  369. default:
  370. dev_warn(&ucs->interface->dev,
  371. "unknown pending request 0x%02x cleared\n",
  372. ucs->pending);
  373. ucs->pending = 0;
  374. }
  375. if (!ucs->pending)
  376. del_timer(&ucs->timer_ctrl);
  377. spin_unlock_irqrestore(&ucs->lock, flags);
  378. }
  379. /* cmd_in_timeout
  380. * timeout routine for command input request
  381. * argument:
  382. * controller state structure
  383. */
  384. static void cmd_in_timeout(unsigned long data)
  385. {
  386. struct cardstate *cs = (struct cardstate *) data;
  387. struct bas_cardstate *ucs = cs->hw.bas;
  388. int rc;
  389. if (!ucs->rcvbuf_size) {
  390. gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
  391. return;
  392. }
  393. if (ucs->retry_cmd_in++ >= BAS_RETRY) {
  394. dev_err(cs->dev,
  395. "control read: timeout, giving up after %d tries\n",
  396. ucs->retry_cmd_in);
  397. kfree(ucs->rcvbuf);
  398. ucs->rcvbuf = NULL;
  399. ucs->rcvbuf_size = 0;
  400. error_reset(cs);
  401. return;
  402. }
  403. gig_dbg(DEBUG_USBREQ, "%s: timeout, retry %d",
  404. __func__, ucs->retry_cmd_in);
  405. rc = atread_submit(cs, BAS_TIMEOUT);
  406. if (rc < 0) {
  407. kfree(ucs->rcvbuf);
  408. ucs->rcvbuf = NULL;
  409. ucs->rcvbuf_size = 0;
  410. if (rc != -ENODEV)
  411. error_reset(cs);
  412. }
  413. }
  414. /* read_ctrl_callback
  415. * USB completion handler for control pipe input
  416. * called by the USB subsystem in interrupt context
  417. * parameter:
  418. * urb USB request block
  419. * urb->context = inbuf structure for controller state
  420. */
  421. static void read_ctrl_callback(struct urb *urb)
  422. {
  423. struct inbuf_t *inbuf = urb->context;
  424. struct cardstate *cs = inbuf->cs;
  425. struct bas_cardstate *ucs = cs->hw.bas;
  426. int status = urb->status;
  427. unsigned numbytes;
  428. int rc;
  429. update_basstate(ucs, 0, BS_ATRDPEND);
  430. wake_up(&ucs->waitqueue);
  431. del_timer(&ucs->timer_cmd_in);
  432. switch (status) {
  433. case 0: /* normal completion */
  434. numbytes = urb->actual_length;
  435. if (unlikely(numbytes != ucs->rcvbuf_size)) {
  436. dev_warn(cs->dev,
  437. "control read: received %d chars, expected %d\n",
  438. numbytes, ucs->rcvbuf_size);
  439. if (numbytes > ucs->rcvbuf_size)
  440. numbytes = ucs->rcvbuf_size;
  441. }
  442. /* copy received bytes to inbuf, notify event layer */
  443. if (gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes)) {
  444. gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
  445. gigaset_schedule_event(cs);
  446. }
  447. break;
  448. case -ENOENT: /* cancelled */
  449. case -ECONNRESET: /* cancelled (async) */
  450. case -EINPROGRESS: /* pending */
  451. case -ENODEV: /* device removed */
  452. case -ESHUTDOWN: /* device shut down */
  453. /* no further action necessary */
  454. gig_dbg(DEBUG_USBREQ, "%s: %s",
  455. __func__, get_usb_statmsg(status));
  456. break;
  457. default: /* other errors: retry */
  458. if (ucs->retry_cmd_in++ < BAS_RETRY) {
  459. gig_dbg(DEBUG_USBREQ, "%s: %s, retry %d", __func__,
  460. get_usb_statmsg(status), ucs->retry_cmd_in);
  461. rc = atread_submit(cs, BAS_TIMEOUT);
  462. if (rc >= 0)
  463. /* successfully resubmitted, skip freeing */
  464. return;
  465. if (rc == -ENODEV)
  466. /* disconnect, no further action necessary */
  467. break;
  468. }
  469. dev_err(cs->dev, "control read: %s, giving up after %d tries\n",
  470. get_usb_statmsg(status), ucs->retry_cmd_in);
  471. error_reset(cs);
  472. }
  473. /* read finished, free buffer */
  474. kfree(ucs->rcvbuf);
  475. ucs->rcvbuf = NULL;
  476. ucs->rcvbuf_size = 0;
  477. }
  478. /* atread_submit
  479. * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
  480. * parameters:
  481. * cs controller state structure
  482. * timeout timeout in 1/10 sec., 0: none
  483. * return value:
  484. * 0 on success
  485. * -EBUSY if another request is pending
  486. * any URB submission error code
  487. */
  488. static int atread_submit(struct cardstate *cs, int timeout)
  489. {
  490. struct bas_cardstate *ucs = cs->hw.bas;
  491. int basstate;
  492. int ret;
  493. gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
  494. ucs->rcvbuf_size);
  495. basstate = update_basstate(ucs, BS_ATRDPEND, 0);
  496. if (basstate & BS_ATRDPEND) {
  497. dev_err(cs->dev,
  498. "could not submit HD_READ_ATMESSAGE: URB busy\n");
  499. return -EBUSY;
  500. }
  501. if (basstate & BS_SUSPEND) {
  502. dev_notice(cs->dev,
  503. "HD_READ_ATMESSAGE not submitted, "
  504. "suspend in progress\n");
  505. update_basstate(ucs, 0, BS_ATRDPEND);
  506. /* treat like disconnect */
  507. return -ENODEV;
  508. }
  509. ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
  510. ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
  511. ucs->dr_cmd_in.wValue = 0;
  512. ucs->dr_cmd_in.wIndex = 0;
  513. ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
  514. usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
  515. usb_rcvctrlpipe(ucs->udev, 0),
  516. (unsigned char *) &ucs->dr_cmd_in,
  517. ucs->rcvbuf, ucs->rcvbuf_size,
  518. read_ctrl_callback, cs->inbuf);
  519. ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
  520. if (ret != 0) {
  521. update_basstate(ucs, 0, BS_ATRDPEND);
  522. dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
  523. get_usb_rcmsg(ret));
  524. return ret;
  525. }
  526. if (timeout > 0) {
  527. gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
  528. mod_timer(&ucs->timer_cmd_in, jiffies + timeout * HZ / 10);
  529. }
  530. return 0;
  531. }
  532. /* read_int_callback
  533. * USB completion handler for interrupt pipe input
  534. * called by the USB subsystem in interrupt context
  535. * parameter:
  536. * urb USB request block
  537. * urb->context = controller state structure
  538. */
  539. static void read_int_callback(struct urb *urb)
  540. {
  541. struct cardstate *cs = urb->context;
  542. struct bas_cardstate *ucs = cs->hw.bas;
  543. struct bc_state *bcs;
  544. int status = urb->status;
  545. unsigned long flags;
  546. int rc;
  547. unsigned l;
  548. int channel;
  549. switch (status) {
  550. case 0: /* success */
  551. break;
  552. case -ENOENT: /* cancelled */
  553. case -ECONNRESET: /* cancelled (async) */
  554. case -EINPROGRESS: /* pending */
  555. /* ignore silently */
  556. gig_dbg(DEBUG_USBREQ, "%s: %s",
  557. __func__, get_usb_statmsg(status));
  558. return;
  559. case -ENODEV: /* device removed */
  560. case -ESHUTDOWN: /* device shut down */
  561. gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
  562. return;
  563. default: /* severe trouble */
  564. dev_warn(cs->dev, "interrupt read: %s\n",
  565. get_usb_statmsg(status));
  566. goto resubmit;
  567. }
  568. /* drop incomplete packets even if the missing bytes wouldn't matter */
  569. if (unlikely(urb->actual_length < IP_MSGSIZE)) {
  570. dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
  571. urb->actual_length);
  572. goto resubmit;
  573. }
  574. l = (unsigned) ucs->int_in_buf[1] +
  575. (((unsigned) ucs->int_in_buf[2]) << 8);
  576. gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
  577. urb->actual_length, (int)ucs->int_in_buf[0], l,
  578. (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
  579. channel = 0;
  580. switch (ucs->int_in_buf[0]) {
  581. case HD_DEVICE_INIT_OK:
  582. update_basstate(ucs, BS_INIT, 0);
  583. break;
  584. case HD_READY_SEND_ATDATA:
  585. del_timer(&ucs->timer_atrdy);
  586. update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
  587. start_cbsend(cs);
  588. break;
  589. case HD_OPEN_B2CHANNEL_ACK:
  590. ++channel;
  591. case HD_OPEN_B1CHANNEL_ACK:
  592. bcs = cs->bcs + channel;
  593. update_basstate(ucs, BS_B1OPEN << channel, 0);
  594. gigaset_bchannel_up(bcs);
  595. break;
  596. case HD_OPEN_ATCHANNEL_ACK:
  597. update_basstate(ucs, BS_ATOPEN, 0);
  598. start_cbsend(cs);
  599. break;
  600. case HD_CLOSE_B2CHANNEL_ACK:
  601. ++channel;
  602. case HD_CLOSE_B1CHANNEL_ACK:
  603. bcs = cs->bcs + channel;
  604. update_basstate(ucs, 0, BS_B1OPEN << channel);
  605. stopurbs(bcs->hw.bas);
  606. gigaset_bchannel_down(bcs);
  607. break;
  608. case HD_CLOSE_ATCHANNEL_ACK:
  609. update_basstate(ucs, 0, BS_ATOPEN);
  610. break;
  611. case HD_B2_FLOW_CONTROL:
  612. ++channel;
  613. case HD_B1_FLOW_CONTROL:
  614. bcs = cs->bcs + channel;
  615. atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
  616. &bcs->hw.bas->corrbytes);
  617. gig_dbg(DEBUG_ISO,
  618. "Flow control (channel %d, sub %d): 0x%02x => %d",
  619. channel, bcs->hw.bas->numsub, l,
  620. atomic_read(&bcs->hw.bas->corrbytes));
  621. break;
  622. case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
  623. if (!l) {
  624. dev_warn(cs->dev,
  625. "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
  626. break;
  627. }
  628. spin_lock_irqsave(&cs->lock, flags);
  629. if (ucs->rcvbuf_size) {
  630. /* throw away previous buffer - we have no queue */
  631. dev_err(cs->dev,
  632. "receive AT data overrun, %d bytes lost\n",
  633. ucs->rcvbuf_size);
  634. kfree(ucs->rcvbuf);
  635. ucs->rcvbuf_size = 0;
  636. }
  637. ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
  638. if (ucs->rcvbuf == NULL) {
  639. spin_unlock_irqrestore(&cs->lock, flags);
  640. dev_err(cs->dev, "out of memory receiving AT data\n");
  641. error_reset(cs);
  642. break;
  643. }
  644. ucs->rcvbuf_size = l;
  645. ucs->retry_cmd_in = 0;
  646. rc = atread_submit(cs, BAS_TIMEOUT);
  647. if (rc < 0) {
  648. kfree(ucs->rcvbuf);
  649. ucs->rcvbuf = NULL;
  650. ucs->rcvbuf_size = 0;
  651. if (rc != -ENODEV) {
  652. spin_unlock_irqrestore(&cs->lock, flags);
  653. error_reset(cs);
  654. break;
  655. }
  656. }
  657. spin_unlock_irqrestore(&cs->lock, flags);
  658. break;
  659. case HD_RESET_INTERRUPT_PIPE_ACK:
  660. update_basstate(ucs, 0, BS_RESETTING);
  661. dev_notice(cs->dev, "interrupt pipe reset\n");
  662. break;
  663. case HD_SUSPEND_END:
  664. gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
  665. break;
  666. default:
  667. dev_warn(cs->dev,
  668. "unknown Gigaset signal 0x%02x (%u) ignored\n",
  669. (int) ucs->int_in_buf[0], l);
  670. }
  671. check_pending(ucs);
  672. wake_up(&ucs->waitqueue);
  673. resubmit:
  674. rc = usb_submit_urb(urb, GFP_ATOMIC);
  675. if (unlikely(rc != 0 && rc != -ENODEV)) {
  676. dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
  677. get_usb_rcmsg(rc));
  678. error_reset(cs);
  679. }
  680. }
  681. /* read_iso_callback
  682. * USB completion handler for B channel isochronous input
  683. * called by the USB subsystem in interrupt context
  684. * parameter:
  685. * urb USB request block of completed request
  686. * urb->context = bc_state structure
  687. */
  688. static void read_iso_callback(struct urb *urb)
  689. {
  690. struct bc_state *bcs;
  691. struct bas_bc_state *ubc;
  692. int status = urb->status;
  693. unsigned long flags;
  694. int i, rc;
  695. /* status codes not worth bothering the tasklet with */
  696. if (unlikely(status == -ENOENT ||
  697. status == -ECONNRESET ||
  698. status == -EINPROGRESS ||
  699. status == -ENODEV ||
  700. status == -ESHUTDOWN)) {
  701. gig_dbg(DEBUG_ISO, "%s: %s",
  702. __func__, get_usb_statmsg(status));
  703. return;
  704. }
  705. bcs = urb->context;
  706. ubc = bcs->hw.bas;
  707. spin_lock_irqsave(&ubc->isoinlock, flags);
  708. if (likely(ubc->isoindone == NULL)) {
  709. /* pass URB to tasklet */
  710. ubc->isoindone = urb;
  711. ubc->isoinstatus = status;
  712. tasklet_hi_schedule(&ubc->rcvd_tasklet);
  713. } else {
  714. /* tasklet still busy, drop data and resubmit URB */
  715. ubc->loststatus = status;
  716. for (i = 0; i < BAS_NUMFRAMES; i++) {
  717. ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
  718. if (unlikely(urb->iso_frame_desc[i].status != 0 &&
  719. urb->iso_frame_desc[i].status !=
  720. -EINPROGRESS))
  721. ubc->loststatus = urb->iso_frame_desc[i].status;
  722. urb->iso_frame_desc[i].status = 0;
  723. urb->iso_frame_desc[i].actual_length = 0;
  724. }
  725. if (likely(ubc->running)) {
  726. /* urb->dev is clobbered by USB subsystem */
  727. urb->dev = bcs->cs->hw.bas->udev;
  728. urb->transfer_flags = URB_ISO_ASAP;
  729. urb->number_of_packets = BAS_NUMFRAMES;
  730. gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
  731. __func__);
  732. rc = usb_submit_urb(urb, GFP_ATOMIC);
  733. if (unlikely(rc != 0 && rc != -ENODEV)) {
  734. dev_err(bcs->cs->dev,
  735. "could not resubmit isochronous read "
  736. "URB: %s\n", get_usb_rcmsg(rc));
  737. dump_urb(DEBUG_ISO, "isoc read", urb);
  738. error_hangup(bcs);
  739. }
  740. }
  741. }
  742. spin_unlock_irqrestore(&ubc->isoinlock, flags);
  743. }
  744. /* write_iso_callback
  745. * USB completion handler for B channel isochronous output
  746. * called by the USB subsystem in interrupt context
  747. * parameter:
  748. * urb USB request block of completed request
  749. * urb->context = isow_urbctx_t structure
  750. */
  751. static void write_iso_callback(struct urb *urb)
  752. {
  753. struct isow_urbctx_t *ucx;
  754. struct bas_bc_state *ubc;
  755. int status = urb->status;
  756. unsigned long flags;
  757. /* status codes not worth bothering the tasklet with */
  758. if (unlikely(status == -ENOENT ||
  759. status == -ECONNRESET ||
  760. status == -EINPROGRESS ||
  761. status == -ENODEV ||
  762. status == -ESHUTDOWN)) {
  763. gig_dbg(DEBUG_ISO, "%s: %s",
  764. __func__, get_usb_statmsg(status));
  765. return;
  766. }
  767. /* pass URB context to tasklet */
  768. ucx = urb->context;
  769. ubc = ucx->bcs->hw.bas;
  770. ucx->status = status;
  771. spin_lock_irqsave(&ubc->isooutlock, flags);
  772. ubc->isooutovfl = ubc->isooutdone;
  773. ubc->isooutdone = ucx;
  774. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  775. tasklet_hi_schedule(&ubc->sent_tasklet);
  776. }
  777. /* starturbs
  778. * prepare and submit USB request blocks for isochronous input and output
  779. * argument:
  780. * B channel control structure
  781. * return value:
  782. * 0 on success
  783. * < 0 on error (no URBs submitted)
  784. */
  785. static int starturbs(struct bc_state *bcs)
  786. {
  787. struct bas_bc_state *ubc = bcs->hw.bas;
  788. struct urb *urb;
  789. int j, k;
  790. int rc;
  791. /* initialize L2 reception */
  792. if (bcs->proto2 == L2_HDLC)
  793. bcs->inputstate |= INS_flag_hunt;
  794. /* submit all isochronous input URBs */
  795. ubc->running = 1;
  796. for (k = 0; k < BAS_INURBS; k++) {
  797. urb = ubc->isoinurbs[k];
  798. if (!urb) {
  799. rc = -EFAULT;
  800. goto error;
  801. }
  802. urb->dev = bcs->cs->hw.bas->udev;
  803. urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
  804. urb->transfer_flags = URB_ISO_ASAP;
  805. urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
  806. urb->transfer_buffer_length = BAS_INBUFSIZE;
  807. urb->number_of_packets = BAS_NUMFRAMES;
  808. urb->interval = BAS_FRAMETIME;
  809. urb->complete = read_iso_callback;
  810. urb->context = bcs;
  811. for (j = 0; j < BAS_NUMFRAMES; j++) {
  812. urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
  813. urb->iso_frame_desc[j].length = BAS_MAXFRAME;
  814. urb->iso_frame_desc[j].status = 0;
  815. urb->iso_frame_desc[j].actual_length = 0;
  816. }
  817. dump_urb(DEBUG_ISO, "Initial isoc read", urb);
  818. rc = usb_submit_urb(urb, GFP_ATOMIC);
  819. if (rc != 0)
  820. goto error;
  821. }
  822. /* initialize L2 transmission */
  823. gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
  824. /* set up isochronous output URBs for flag idling */
  825. for (k = 0; k < BAS_OUTURBS; ++k) {
  826. urb = ubc->isoouturbs[k].urb;
  827. if (!urb) {
  828. rc = -EFAULT;
  829. goto error;
  830. }
  831. urb->dev = bcs->cs->hw.bas->udev;
  832. urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
  833. urb->transfer_flags = URB_ISO_ASAP;
  834. urb->transfer_buffer = ubc->isooutbuf->data;
  835. urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
  836. urb->number_of_packets = BAS_NUMFRAMES;
  837. urb->interval = BAS_FRAMETIME;
  838. urb->complete = write_iso_callback;
  839. urb->context = &ubc->isoouturbs[k];
  840. for (j = 0; j < BAS_NUMFRAMES; ++j) {
  841. urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
  842. urb->iso_frame_desc[j].length = BAS_NORMFRAME;
  843. urb->iso_frame_desc[j].status = 0;
  844. urb->iso_frame_desc[j].actual_length = 0;
  845. }
  846. ubc->isoouturbs[k].limit = -1;
  847. }
  848. /* keep one URB free, submit the others */
  849. for (k = 0; k < BAS_OUTURBS-1; ++k) {
  850. dump_urb(DEBUG_ISO, "Initial isoc write", urb);
  851. rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
  852. if (rc != 0)
  853. goto error;
  854. }
  855. dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
  856. ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
  857. ubc->isooutdone = ubc->isooutovfl = NULL;
  858. return 0;
  859. error:
  860. stopurbs(ubc);
  861. return rc;
  862. }
  863. /* stopurbs
  864. * cancel the USB request blocks for isochronous input and output
  865. * errors are silently ignored
  866. * argument:
  867. * B channel control structure
  868. */
  869. static void stopurbs(struct bas_bc_state *ubc)
  870. {
  871. int k, rc;
  872. ubc->running = 0;
  873. for (k = 0; k < BAS_INURBS; ++k) {
  874. rc = usb_unlink_urb(ubc->isoinurbs[k]);
  875. gig_dbg(DEBUG_ISO,
  876. "%s: isoc input URB %d unlinked, result = %s",
  877. __func__, k, get_usb_rcmsg(rc));
  878. }
  879. for (k = 0; k < BAS_OUTURBS; ++k) {
  880. rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
  881. gig_dbg(DEBUG_ISO,
  882. "%s: isoc output URB %d unlinked, result = %s",
  883. __func__, k, get_usb_rcmsg(rc));
  884. }
  885. }
  886. /* Isochronous Write - Bottom Half */
  887. /* =============================== */
  888. /* submit_iso_write_urb
  889. * fill and submit the next isochronous write URB
  890. * parameters:
  891. * ucx context structure containing URB
  892. * return value:
  893. * number of frames submitted in URB
  894. * 0 if URB not submitted because no data available (isooutbuf busy)
  895. * error code < 0 on error
  896. */
  897. static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
  898. {
  899. struct urb *urb = ucx->urb;
  900. struct bas_bc_state *ubc = ucx->bcs->hw.bas;
  901. struct usb_iso_packet_descriptor *ifd;
  902. int corrbytes, nframe, rc;
  903. /* urb->dev is clobbered by USB subsystem */
  904. urb->dev = ucx->bcs->cs->hw.bas->udev;
  905. urb->transfer_flags = URB_ISO_ASAP;
  906. urb->transfer_buffer = ubc->isooutbuf->data;
  907. urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
  908. for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
  909. ifd = &urb->iso_frame_desc[nframe];
  910. /* compute frame length according to flow control */
  911. ifd->length = BAS_NORMFRAME;
  912. corrbytes = atomic_read(&ubc->corrbytes);
  913. if (corrbytes != 0) {
  914. gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
  915. __func__, corrbytes);
  916. if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
  917. corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
  918. else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
  919. corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
  920. ifd->length += corrbytes;
  921. atomic_add(-corrbytes, &ubc->corrbytes);
  922. }
  923. /* retrieve block of data to send */
  924. rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
  925. if (rc < 0) {
  926. if (rc == -EBUSY) {
  927. gig_dbg(DEBUG_ISO,
  928. "%s: buffer busy at frame %d",
  929. __func__, nframe);
  930. /* tasklet will be restarted from
  931. gigaset_isoc_send_skb() */
  932. } else {
  933. dev_err(ucx->bcs->cs->dev,
  934. "%s: buffer error %d at frame %d\n",
  935. __func__, rc, nframe);
  936. return rc;
  937. }
  938. break;
  939. }
  940. ifd->offset = rc;
  941. ucx->limit = ubc->isooutbuf->nextread;
  942. ifd->status = 0;
  943. ifd->actual_length = 0;
  944. }
  945. if (unlikely(nframe == 0))
  946. return 0; /* no data to send */
  947. urb->number_of_packets = nframe;
  948. rc = usb_submit_urb(urb, GFP_ATOMIC);
  949. if (unlikely(rc)) {
  950. if (rc == -ENODEV)
  951. /* device removed - give up silently */
  952. gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
  953. else
  954. dev_err(ucx->bcs->cs->dev,
  955. "could not submit isochronous write URB: %s\n",
  956. get_usb_rcmsg(rc));
  957. return rc;
  958. }
  959. ++ubc->numsub;
  960. return nframe;
  961. }
  962. /* write_iso_tasklet
  963. * tasklet scheduled when an isochronous output URB from the Gigaset device
  964. * has completed
  965. * parameter:
  966. * data B channel state structure
  967. */
  968. static void write_iso_tasklet(unsigned long data)
  969. {
  970. struct bc_state *bcs = (struct bc_state *) data;
  971. struct bas_bc_state *ubc = bcs->hw.bas;
  972. struct cardstate *cs = bcs->cs;
  973. struct isow_urbctx_t *done, *next, *ovfl;
  974. struct urb *urb;
  975. int status;
  976. struct usb_iso_packet_descriptor *ifd;
  977. int offset;
  978. unsigned long flags;
  979. int i;
  980. struct sk_buff *skb;
  981. int len;
  982. int rc;
  983. /* loop while completed URBs arrive in time */
  984. for (;;) {
  985. if (unlikely(!(ubc->running))) {
  986. gig_dbg(DEBUG_ISO, "%s: not running", __func__);
  987. return;
  988. }
  989. /* retrieve completed URBs */
  990. spin_lock_irqsave(&ubc->isooutlock, flags);
  991. done = ubc->isooutdone;
  992. ubc->isooutdone = NULL;
  993. ovfl = ubc->isooutovfl;
  994. ubc->isooutovfl = NULL;
  995. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  996. if (ovfl) {
  997. dev_err(cs->dev, "isochronous write buffer underrun\n");
  998. error_hangup(bcs);
  999. break;
  1000. }
  1001. if (!done)
  1002. break;
  1003. /* submit free URB if available */
  1004. spin_lock_irqsave(&ubc->isooutlock, flags);
  1005. next = ubc->isooutfree;
  1006. ubc->isooutfree = NULL;
  1007. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1008. if (next) {
  1009. rc = submit_iso_write_urb(next);
  1010. if (unlikely(rc <= 0 && rc != -ENODEV)) {
  1011. /* could not submit URB, put it back */
  1012. spin_lock_irqsave(&ubc->isooutlock, flags);
  1013. if (ubc->isooutfree == NULL) {
  1014. ubc->isooutfree = next;
  1015. next = NULL;
  1016. }
  1017. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1018. if (next) {
  1019. /* couldn't put it back */
  1020. dev_err(cs->dev,
  1021. "losing isochronous write URB\n");
  1022. error_hangup(bcs);
  1023. }
  1024. }
  1025. }
  1026. /* process completed URB */
  1027. urb = done->urb;
  1028. status = done->status;
  1029. switch (status) {
  1030. case -EXDEV: /* partial completion */
  1031. gig_dbg(DEBUG_ISO, "%s: URB partially completed",
  1032. __func__);
  1033. /* fall through - what's the difference anyway? */
  1034. case 0: /* normal completion */
  1035. /* inspect individual frames
  1036. * assumptions (for lack of documentation):
  1037. * - actual_length bytes of first frame in error are
  1038. * successfully sent
  1039. * - all following frames are not sent at all
  1040. */
  1041. offset = done->limit; /* default (no error) */
  1042. for (i = 0; i < BAS_NUMFRAMES; i++) {
  1043. ifd = &urb->iso_frame_desc[i];
  1044. if (ifd->status ||
  1045. ifd->actual_length != ifd->length) {
  1046. dev_warn(cs->dev,
  1047. "isochronous write: frame %d: %s, "
  1048. "only %d of %d bytes sent\n",
  1049. i, get_usb_statmsg(ifd->status),
  1050. ifd->actual_length, ifd->length);
  1051. offset = (ifd->offset +
  1052. ifd->actual_length)
  1053. % BAS_OUTBUFSIZE;
  1054. break;
  1055. }
  1056. }
  1057. break;
  1058. case -EPIPE: /* stall - probably underrun */
  1059. dev_err(cs->dev, "isochronous write stalled\n");
  1060. error_hangup(bcs);
  1061. break;
  1062. default: /* severe trouble */
  1063. dev_warn(cs->dev, "isochronous write: %s\n",
  1064. get_usb_statmsg(status));
  1065. }
  1066. /* mark the write buffer area covered by this URB as free */
  1067. if (done->limit >= 0)
  1068. ubc->isooutbuf->read = done->limit;
  1069. /* mark URB as free */
  1070. spin_lock_irqsave(&ubc->isooutlock, flags);
  1071. next = ubc->isooutfree;
  1072. ubc->isooutfree = done;
  1073. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1074. if (next) {
  1075. /* only one URB still active - resubmit one */
  1076. rc = submit_iso_write_urb(next);
  1077. if (unlikely(rc <= 0 && rc != -ENODEV)) {
  1078. /* couldn't submit */
  1079. error_hangup(bcs);
  1080. }
  1081. }
  1082. }
  1083. /* process queued SKBs */
  1084. while ((skb = skb_dequeue(&bcs->squeue))) {
  1085. /* copy to output buffer, doing L2 encapsulation */
  1086. len = skb->len;
  1087. if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
  1088. /* insufficient buffer space, push back onto queue */
  1089. skb_queue_head(&bcs->squeue, skb);
  1090. gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
  1091. __func__, skb_queue_len(&bcs->squeue));
  1092. break;
  1093. }
  1094. skb_pull(skb, len);
  1095. gigaset_skb_sent(bcs, skb);
  1096. dev_kfree_skb_any(skb);
  1097. }
  1098. }
  1099. /* Isochronous Read - Bottom Half */
  1100. /* ============================== */
  1101. /* read_iso_tasklet
  1102. * tasklet scheduled when an isochronous input URB from the Gigaset device
  1103. * has completed
  1104. * parameter:
  1105. * data B channel state structure
  1106. */
  1107. static void read_iso_tasklet(unsigned long data)
  1108. {
  1109. struct bc_state *bcs = (struct bc_state *) data;
  1110. struct bas_bc_state *ubc = bcs->hw.bas;
  1111. struct cardstate *cs = bcs->cs;
  1112. struct urb *urb;
  1113. int status;
  1114. char *rcvbuf;
  1115. unsigned long flags;
  1116. int totleft, numbytes, offset, frame, rc;
  1117. /* loop while more completed URBs arrive in the meantime */
  1118. for (;;) {
  1119. /* retrieve URB */
  1120. spin_lock_irqsave(&ubc->isoinlock, flags);
  1121. urb = ubc->isoindone;
  1122. if (!urb) {
  1123. spin_unlock_irqrestore(&ubc->isoinlock, flags);
  1124. return;
  1125. }
  1126. status = ubc->isoinstatus;
  1127. ubc->isoindone = NULL;
  1128. if (unlikely(ubc->loststatus != -EINPROGRESS)) {
  1129. dev_warn(cs->dev,
  1130. "isochronous read overrun, "
  1131. "dropped URB with status: %s, %d bytes lost\n",
  1132. get_usb_statmsg(ubc->loststatus),
  1133. ubc->isoinlost);
  1134. ubc->loststatus = -EINPROGRESS;
  1135. }
  1136. spin_unlock_irqrestore(&ubc->isoinlock, flags);
  1137. if (unlikely(!(ubc->running))) {
  1138. gig_dbg(DEBUG_ISO,
  1139. "%s: channel not running, "
  1140. "dropped URB with status: %s",
  1141. __func__, get_usb_statmsg(status));
  1142. return;
  1143. }
  1144. switch (status) {
  1145. case 0: /* normal completion */
  1146. break;
  1147. case -EXDEV: /* inspect individual frames
  1148. (we do that anyway) */
  1149. gig_dbg(DEBUG_ISO, "%s: URB partially completed",
  1150. __func__);
  1151. break;
  1152. case -ENOENT:
  1153. case -ECONNRESET:
  1154. case -EINPROGRESS:
  1155. gig_dbg(DEBUG_ISO, "%s: %s",
  1156. __func__, get_usb_statmsg(status));
  1157. continue; /* -> skip */
  1158. case -EPIPE:
  1159. dev_err(cs->dev, "isochronous read stalled\n");
  1160. error_hangup(bcs);
  1161. continue; /* -> skip */
  1162. default: /* severe trouble */
  1163. dev_warn(cs->dev, "isochronous read: %s\n",
  1164. get_usb_statmsg(status));
  1165. goto error;
  1166. }
  1167. rcvbuf = urb->transfer_buffer;
  1168. totleft = urb->actual_length;
  1169. for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
  1170. numbytes = urb->iso_frame_desc[frame].actual_length;
  1171. if (unlikely(urb->iso_frame_desc[frame].status))
  1172. dev_warn(cs->dev,
  1173. "isochronous read: frame %d[%d]: %s\n",
  1174. frame, numbytes,
  1175. get_usb_statmsg(
  1176. urb->iso_frame_desc[frame].status));
  1177. if (unlikely(numbytes > BAS_MAXFRAME))
  1178. dev_warn(cs->dev,
  1179. "isochronous read: frame %d: "
  1180. "numbytes (%d) > BAS_MAXFRAME\n",
  1181. frame, numbytes);
  1182. if (unlikely(numbytes > totleft)) {
  1183. dev_warn(cs->dev,
  1184. "isochronous read: frame %d: "
  1185. "numbytes (%d) > totleft (%d)\n",
  1186. frame, numbytes, totleft);
  1187. numbytes = totleft;
  1188. }
  1189. offset = urb->iso_frame_desc[frame].offset;
  1190. if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
  1191. dev_warn(cs->dev,
  1192. "isochronous read: frame %d: "
  1193. "offset (%d) + numbytes (%d) "
  1194. "> BAS_INBUFSIZE\n",
  1195. frame, offset, numbytes);
  1196. numbytes = BAS_INBUFSIZE - offset;
  1197. }
  1198. gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
  1199. totleft -= numbytes;
  1200. }
  1201. if (unlikely(totleft > 0))
  1202. dev_warn(cs->dev,
  1203. "isochronous read: %d data bytes missing\n",
  1204. totleft);
  1205. error:
  1206. /* URB processed, resubmit */
  1207. for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
  1208. urb->iso_frame_desc[frame].status = 0;
  1209. urb->iso_frame_desc[frame].actual_length = 0;
  1210. }
  1211. /* urb->dev is clobbered by USB subsystem */
  1212. urb->dev = bcs->cs->hw.bas->udev;
  1213. urb->transfer_flags = URB_ISO_ASAP;
  1214. urb->number_of_packets = BAS_NUMFRAMES;
  1215. rc = usb_submit_urb(urb, GFP_ATOMIC);
  1216. if (unlikely(rc != 0 && rc != -ENODEV)) {
  1217. dev_err(cs->dev,
  1218. "could not resubmit isochronous read URB: %s\n",
  1219. get_usb_rcmsg(rc));
  1220. dump_urb(DEBUG_ISO, "resubmit iso read", urb);
  1221. error_hangup(bcs);
  1222. }
  1223. }
  1224. }
  1225. /* Channel Operations */
  1226. /* ================== */
  1227. /* req_timeout
  1228. * timeout routine for control output request
  1229. * argument:
  1230. * controller state structure
  1231. */
  1232. static void req_timeout(unsigned long data)
  1233. {
  1234. struct cardstate *cs = (struct cardstate *) data;
  1235. struct bas_cardstate *ucs = cs->hw.bas;
  1236. int pending;
  1237. unsigned long flags;
  1238. check_pending(ucs);
  1239. spin_lock_irqsave(&ucs->lock, flags);
  1240. pending = ucs->pending;
  1241. ucs->pending = 0;
  1242. spin_unlock_irqrestore(&ucs->lock, flags);
  1243. switch (pending) {
  1244. case 0: /* no pending request */
  1245. gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
  1246. break;
  1247. case HD_OPEN_ATCHANNEL:
  1248. dev_err(cs->dev, "timeout opening AT channel\n");
  1249. error_reset(cs);
  1250. break;
  1251. case HD_OPEN_B1CHANNEL:
  1252. dev_err(cs->dev, "timeout opening channel 1\n");
  1253. error_hangup(&cs->bcs[0]);
  1254. break;
  1255. case HD_OPEN_B2CHANNEL:
  1256. dev_err(cs->dev, "timeout opening channel 2\n");
  1257. error_hangup(&cs->bcs[1]);
  1258. break;
  1259. case HD_CLOSE_ATCHANNEL:
  1260. dev_err(cs->dev, "timeout closing AT channel\n");
  1261. error_reset(cs);
  1262. break;
  1263. case HD_CLOSE_B1CHANNEL:
  1264. dev_err(cs->dev, "timeout closing channel 1\n");
  1265. error_reset(cs);
  1266. break;
  1267. case HD_CLOSE_B2CHANNEL:
  1268. dev_err(cs->dev, "timeout closing channel 2\n");
  1269. error_reset(cs);
  1270. break;
  1271. case HD_RESET_INTERRUPT_PIPE:
  1272. /* error recovery escalation */
  1273. dev_err(cs->dev,
  1274. "reset interrupt pipe timeout, attempting USB reset\n");
  1275. usb_queue_reset_device(ucs->interface);
  1276. break;
  1277. default:
  1278. dev_warn(cs->dev, "request 0x%02x timed out, clearing\n",
  1279. pending);
  1280. }
  1281. wake_up(&ucs->waitqueue);
  1282. }
  1283. /* write_ctrl_callback
  1284. * USB completion handler for control pipe output
  1285. * called by the USB subsystem in interrupt context
  1286. * parameter:
  1287. * urb USB request block of completed request
  1288. * urb->context = hardware specific controller state structure
  1289. */
  1290. static void write_ctrl_callback(struct urb *urb)
  1291. {
  1292. struct bas_cardstate *ucs = urb->context;
  1293. int status = urb->status;
  1294. int rc;
  1295. unsigned long flags;
  1296. /* check status */
  1297. switch (status) {
  1298. case 0: /* normal completion */
  1299. spin_lock_irqsave(&ucs->lock, flags);
  1300. switch (ucs->pending) {
  1301. case HD_DEVICE_INIT_ACK: /* no reply expected */
  1302. del_timer(&ucs->timer_ctrl);
  1303. ucs->pending = 0;
  1304. break;
  1305. }
  1306. spin_unlock_irqrestore(&ucs->lock, flags);
  1307. return;
  1308. case -ENOENT: /* cancelled */
  1309. case -ECONNRESET: /* cancelled (async) */
  1310. case -EINPROGRESS: /* pending */
  1311. case -ENODEV: /* device removed */
  1312. case -ESHUTDOWN: /* device shut down */
  1313. /* ignore silently */
  1314. gig_dbg(DEBUG_USBREQ, "%s: %s",
  1315. __func__, get_usb_statmsg(status));
  1316. break;
  1317. default: /* any failure */
  1318. /* don't retry if suspend requested */
  1319. if (++ucs->retry_ctrl > BAS_RETRY ||
  1320. (ucs->basstate & BS_SUSPEND)) {
  1321. dev_err(&ucs->interface->dev,
  1322. "control request 0x%02x failed: %s\n",
  1323. ucs->dr_ctrl.bRequest,
  1324. get_usb_statmsg(status));
  1325. break; /* give up */
  1326. }
  1327. dev_notice(&ucs->interface->dev,
  1328. "control request 0x%02x: %s, retry %d\n",
  1329. ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
  1330. ucs->retry_ctrl);
  1331. /* urb->dev is clobbered by USB subsystem */
  1332. urb->dev = ucs->udev;
  1333. rc = usb_submit_urb(urb, GFP_ATOMIC);
  1334. if (unlikely(rc)) {
  1335. dev_err(&ucs->interface->dev,
  1336. "could not resubmit request 0x%02x: %s\n",
  1337. ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
  1338. break;
  1339. }
  1340. /* resubmitted */
  1341. return;
  1342. }
  1343. /* failed, clear pending request */
  1344. spin_lock_irqsave(&ucs->lock, flags);
  1345. del_timer(&ucs->timer_ctrl);
  1346. ucs->pending = 0;
  1347. spin_unlock_irqrestore(&ucs->lock, flags);
  1348. wake_up(&ucs->waitqueue);
  1349. }
  1350. /* req_submit
  1351. * submit a control output request without message buffer to the Gigaset base
  1352. * and optionally start a timeout
  1353. * parameters:
  1354. * bcs B channel control structure
  1355. * req control request code (HD_*)
  1356. * val control request parameter value (set to 0 if unused)
  1357. * timeout timeout in seconds (0: no timeout)
  1358. * return value:
  1359. * 0 on success
  1360. * -EBUSY if another request is pending
  1361. * any URB submission error code
  1362. */
  1363. static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
  1364. {
  1365. struct bas_cardstate *ucs = bcs->cs->hw.bas;
  1366. int ret;
  1367. unsigned long flags;
  1368. gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
  1369. spin_lock_irqsave(&ucs->lock, flags);
  1370. if (ucs->pending) {
  1371. spin_unlock_irqrestore(&ucs->lock, flags);
  1372. dev_err(bcs->cs->dev,
  1373. "submission of request 0x%02x failed: "
  1374. "request 0x%02x still pending\n",
  1375. req, ucs->pending);
  1376. return -EBUSY;
  1377. }
  1378. ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
  1379. ucs->dr_ctrl.bRequest = req;
  1380. ucs->dr_ctrl.wValue = cpu_to_le16(val);
  1381. ucs->dr_ctrl.wIndex = 0;
  1382. ucs->dr_ctrl.wLength = 0;
  1383. usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
  1384. usb_sndctrlpipe(ucs->udev, 0),
  1385. (unsigned char *) &ucs->dr_ctrl, NULL, 0,
  1386. write_ctrl_callback, ucs);
  1387. ucs->retry_ctrl = 0;
  1388. ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
  1389. if (unlikely(ret)) {
  1390. dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
  1391. req, get_usb_rcmsg(ret));
  1392. spin_unlock_irqrestore(&ucs->lock, flags);
  1393. return ret;
  1394. }
  1395. ucs->pending = req;
  1396. if (timeout > 0) {
  1397. gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
  1398. mod_timer(&ucs->timer_ctrl, jiffies + timeout * HZ / 10);
  1399. }
  1400. spin_unlock_irqrestore(&ucs->lock, flags);
  1401. return 0;
  1402. }
  1403. /* gigaset_init_bchannel
  1404. * called by common.c to connect a B channel
  1405. * initialize isochronous I/O and tell the Gigaset base to open the channel
  1406. * argument:
  1407. * B channel control structure
  1408. * return value:
  1409. * 0 on success, error code < 0 on error
  1410. */
  1411. static int gigaset_init_bchannel(struct bc_state *bcs)
  1412. {
  1413. struct cardstate *cs = bcs->cs;
  1414. int req, ret;
  1415. unsigned long flags;
  1416. spin_lock_irqsave(&cs->lock, flags);
  1417. if (unlikely(!cs->connected)) {
  1418. gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
  1419. spin_unlock_irqrestore(&cs->lock, flags);
  1420. return -ENODEV;
  1421. }
  1422. if (cs->hw.bas->basstate & BS_SUSPEND) {
  1423. dev_notice(cs->dev,
  1424. "not starting isochronous I/O, "
  1425. "suspend in progress\n");
  1426. spin_unlock_irqrestore(&cs->lock, flags);
  1427. return -EHOSTUNREACH;
  1428. }
  1429. ret = starturbs(bcs);
  1430. if (ret < 0) {
  1431. spin_unlock_irqrestore(&cs->lock, flags);
  1432. dev_err(cs->dev,
  1433. "could not start isochronous I/O for channel B%d: %s\n",
  1434. bcs->channel + 1,
  1435. ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
  1436. if (ret != -ENODEV)
  1437. error_hangup(bcs);
  1438. return ret;
  1439. }
  1440. req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
  1441. ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
  1442. if (ret < 0) {
  1443. dev_err(cs->dev, "could not open channel B%d\n",
  1444. bcs->channel + 1);
  1445. stopurbs(bcs->hw.bas);
  1446. }
  1447. spin_unlock_irqrestore(&cs->lock, flags);
  1448. if (ret < 0 && ret != -ENODEV)
  1449. error_hangup(bcs);
  1450. return ret;
  1451. }
  1452. /* gigaset_close_bchannel
  1453. * called by common.c to disconnect a B channel
  1454. * tell the Gigaset base to close the channel
  1455. * stopping isochronous I/O and LL notification will be done when the
  1456. * acknowledgement for the close arrives
  1457. * argument:
  1458. * B channel control structure
  1459. * return value:
  1460. * 0 on success, error code < 0 on error
  1461. */
  1462. static int gigaset_close_bchannel(struct bc_state *bcs)
  1463. {
  1464. struct cardstate *cs = bcs->cs;
  1465. int req, ret;
  1466. unsigned long flags;
  1467. spin_lock_irqsave(&cs->lock, flags);
  1468. if (unlikely(!cs->connected)) {
  1469. spin_unlock_irqrestore(&cs->lock, flags);
  1470. gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
  1471. return -ENODEV;
  1472. }
  1473. if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
  1474. /* channel not running: just signal common.c */
  1475. spin_unlock_irqrestore(&cs->lock, flags);
  1476. gigaset_bchannel_down(bcs);
  1477. return 0;
  1478. }
  1479. /* channel running: tell device to close it */
  1480. req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
  1481. ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
  1482. if (ret < 0)
  1483. dev_err(cs->dev, "closing channel B%d failed\n",
  1484. bcs->channel + 1);
  1485. spin_unlock_irqrestore(&cs->lock, flags);
  1486. return ret;
  1487. }
  1488. /* Device Operations */
  1489. /* ================= */
  1490. /* complete_cb
  1491. * unqueue first command buffer from queue, waking any sleepers
  1492. * must be called with cs->cmdlock held
  1493. * parameter:
  1494. * cs controller state structure
  1495. */
  1496. static void complete_cb(struct cardstate *cs)
  1497. {
  1498. struct cmdbuf_t *cb = cs->cmdbuf;
  1499. /* unqueue completed buffer */
  1500. cs->cmdbytes -= cs->curlen;
  1501. gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
  1502. cs->curlen, cs->cmdbytes);
  1503. if (cb->next != NULL) {
  1504. cs->cmdbuf = cb->next;
  1505. cs->cmdbuf->prev = NULL;
  1506. cs->curlen = cs->cmdbuf->len;
  1507. } else {
  1508. cs->cmdbuf = NULL;
  1509. cs->lastcmdbuf = NULL;
  1510. cs->curlen = 0;
  1511. }
  1512. if (cb->wake_tasklet)
  1513. tasklet_schedule(cb->wake_tasklet);
  1514. kfree(cb);
  1515. }
  1516. /* write_command_callback
  1517. * USB completion handler for AT command transmission
  1518. * called by the USB subsystem in interrupt context
  1519. * parameter:
  1520. * urb USB request block of completed request
  1521. * urb->context = controller state structure
  1522. */
  1523. static void write_command_callback(struct urb *urb)
  1524. {
  1525. struct cardstate *cs = urb->context;
  1526. struct bas_cardstate *ucs = cs->hw.bas;
  1527. int status = urb->status;
  1528. unsigned long flags;
  1529. update_basstate(ucs, 0, BS_ATWRPEND);
  1530. wake_up(&ucs->waitqueue);
  1531. /* check status */
  1532. switch (status) {
  1533. case 0: /* normal completion */
  1534. break;
  1535. case -ENOENT: /* cancelled */
  1536. case -ECONNRESET: /* cancelled (async) */
  1537. case -EINPROGRESS: /* pending */
  1538. case -ENODEV: /* device removed */
  1539. case -ESHUTDOWN: /* device shut down */
  1540. /* ignore silently */
  1541. gig_dbg(DEBUG_USBREQ, "%s: %s",
  1542. __func__, get_usb_statmsg(status));
  1543. return;
  1544. default: /* any failure */
  1545. if (++ucs->retry_cmd_out > BAS_RETRY) {
  1546. dev_warn(cs->dev,
  1547. "command write: %s, "
  1548. "giving up after %d retries\n",
  1549. get_usb_statmsg(status),
  1550. ucs->retry_cmd_out);
  1551. break;
  1552. }
  1553. if (ucs->basstate & BS_SUSPEND) {
  1554. dev_warn(cs->dev,
  1555. "command write: %s, "
  1556. "won't retry - suspend requested\n",
  1557. get_usb_statmsg(status));
  1558. break;
  1559. }
  1560. if (cs->cmdbuf == NULL) {
  1561. dev_warn(cs->dev,
  1562. "command write: %s, "
  1563. "cannot retry - cmdbuf gone\n",
  1564. get_usb_statmsg(status));
  1565. break;
  1566. }
  1567. dev_notice(cs->dev, "command write: %s, retry %d\n",
  1568. get_usb_statmsg(status), ucs->retry_cmd_out);
  1569. if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
  1570. /* resubmitted - bypass regular exit block */
  1571. return;
  1572. /* command send failed, assume base still waiting */
  1573. update_basstate(ucs, BS_ATREADY, 0);
  1574. }
  1575. spin_lock_irqsave(&cs->cmdlock, flags);
  1576. if (cs->cmdbuf != NULL)
  1577. complete_cb(cs);
  1578. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1579. }
  1580. /* atrdy_timeout
  1581. * timeout routine for AT command transmission
  1582. * argument:
  1583. * controller state structure
  1584. */
  1585. static void atrdy_timeout(unsigned long data)
  1586. {
  1587. struct cardstate *cs = (struct cardstate *) data;
  1588. struct bas_cardstate *ucs = cs->hw.bas;
  1589. dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
  1590. /* fake the missing signal - what else can I do? */
  1591. update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
  1592. start_cbsend(cs);
  1593. }
  1594. /* atwrite_submit
  1595. * submit an HD_WRITE_ATMESSAGE command URB
  1596. * parameters:
  1597. * cs controller state structure
  1598. * buf buffer containing command to send
  1599. * len length of command to send
  1600. * return value:
  1601. * 0 on success
  1602. * -EBUSY if another request is pending
  1603. * any URB submission error code
  1604. */
  1605. static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
  1606. {
  1607. struct bas_cardstate *ucs = cs->hw.bas;
  1608. int rc;
  1609. gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
  1610. if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
  1611. dev_err(cs->dev,
  1612. "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
  1613. return -EBUSY;
  1614. }
  1615. ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
  1616. ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
  1617. ucs->dr_cmd_out.wValue = 0;
  1618. ucs->dr_cmd_out.wIndex = 0;
  1619. ucs->dr_cmd_out.wLength = cpu_to_le16(len);
  1620. usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
  1621. usb_sndctrlpipe(ucs->udev, 0),
  1622. (unsigned char *) &ucs->dr_cmd_out, buf, len,
  1623. write_command_callback, cs);
  1624. rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
  1625. if (unlikely(rc)) {
  1626. update_basstate(ucs, 0, BS_ATWRPEND);
  1627. dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
  1628. get_usb_rcmsg(rc));
  1629. return rc;
  1630. }
  1631. /* submitted successfully, start timeout if necessary */
  1632. if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
  1633. gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
  1634. ATRDY_TIMEOUT);
  1635. mod_timer(&ucs->timer_atrdy, jiffies + ATRDY_TIMEOUT * HZ / 10);
  1636. }
  1637. return 0;
  1638. }
  1639. /* start_cbsend
  1640. * start transmission of AT command queue if necessary
  1641. * parameter:
  1642. * cs controller state structure
  1643. * return value:
  1644. * 0 on success
  1645. * error code < 0 on error
  1646. */
  1647. static int start_cbsend(struct cardstate *cs)
  1648. {
  1649. struct cmdbuf_t *cb;
  1650. struct bas_cardstate *ucs = cs->hw.bas;
  1651. unsigned long flags;
  1652. int rc;
  1653. int retval = 0;
  1654. /* check if suspend requested */
  1655. if (ucs->basstate & BS_SUSPEND) {
  1656. gig_dbg(DEBUG_OUTPUT, "suspending");
  1657. return -EHOSTUNREACH;
  1658. }
  1659. /* check if AT channel is open */
  1660. if (!(ucs->basstate & BS_ATOPEN)) {
  1661. gig_dbg(DEBUG_OUTPUT, "AT channel not open");
  1662. rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
  1663. if (rc < 0) {
  1664. /* flush command queue */
  1665. spin_lock_irqsave(&cs->cmdlock, flags);
  1666. while (cs->cmdbuf != NULL)
  1667. complete_cb(cs);
  1668. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1669. }
  1670. return rc;
  1671. }
  1672. /* try to send first command in queue */
  1673. spin_lock_irqsave(&cs->cmdlock, flags);
  1674. while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
  1675. ucs->retry_cmd_out = 0;
  1676. rc = atwrite_submit(cs, cb->buf, cb->len);
  1677. if (unlikely(rc)) {
  1678. retval = rc;
  1679. complete_cb(cs);
  1680. }
  1681. }
  1682. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1683. return retval;
  1684. }
  1685. /* gigaset_write_cmd
  1686. * This function is called by the device independent part of the driver
  1687. * to transmit an AT command string to the Gigaset device.
  1688. * It encapsulates the device specific method for transmission over the
  1689. * direct USB connection to the base.
  1690. * The command string is added to the queue of commands to send, and
  1691. * USB transmission is started if necessary.
  1692. * parameters:
  1693. * cs controller state structure
  1694. * cb command buffer structure
  1695. * return value:
  1696. * number of bytes queued on success
  1697. * error code < 0 on error
  1698. */
  1699. static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
  1700. {
  1701. unsigned long flags;
  1702. int rc;
  1703. gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
  1704. DEBUG_TRANSCMD : DEBUG_LOCKCMD,
  1705. "CMD Transmit", cb->len, cb->buf);
  1706. /* translate "+++" escape sequence sent as a single separate command
  1707. * into "close AT channel" command for error recovery
  1708. * The next command will reopen the AT channel automatically.
  1709. */
  1710. if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {
  1711. rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
  1712. if (cb->wake_tasklet)
  1713. tasklet_schedule(cb->wake_tasklet);
  1714. if (!rc)
  1715. rc = cb->len;
  1716. kfree(cb);
  1717. return rc;
  1718. }
  1719. spin_lock_irqsave(&cs->cmdlock, flags);
  1720. cb->prev = cs->lastcmdbuf;
  1721. if (cs->lastcmdbuf)
  1722. cs->lastcmdbuf->next = cb;
  1723. else {
  1724. cs->cmdbuf = cb;
  1725. cs->curlen = cb->len;
  1726. }
  1727. cs->cmdbytes += cb->len;
  1728. cs->lastcmdbuf = cb;
  1729. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1730. spin_lock_irqsave(&cs->lock, flags);
  1731. if (unlikely(!cs->connected)) {
  1732. spin_unlock_irqrestore(&cs->lock, flags);
  1733. gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
  1734. /* flush command queue */
  1735. spin_lock_irqsave(&cs->cmdlock, flags);
  1736. while (cs->cmdbuf != NULL)
  1737. complete_cb(cs);
  1738. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1739. return -ENODEV;
  1740. }
  1741. rc = start_cbsend(cs);
  1742. spin_unlock_irqrestore(&cs->lock, flags);
  1743. return rc < 0 ? rc : cb->len;
  1744. }
  1745. /* gigaset_write_room
  1746. * tty_driver.write_room interface routine
  1747. * return number of characters the driver will accept to be written via
  1748. * gigaset_write_cmd
  1749. * parameter:
  1750. * controller state structure
  1751. * return value:
  1752. * number of characters
  1753. */
  1754. static int gigaset_write_room(struct cardstate *cs)
  1755. {
  1756. return IF_WRITEBUF;
  1757. }
  1758. /* gigaset_chars_in_buffer
  1759. * tty_driver.chars_in_buffer interface routine
  1760. * return number of characters waiting to be sent
  1761. * parameter:
  1762. * controller state structure
  1763. * return value:
  1764. * number of characters
  1765. */
  1766. static int gigaset_chars_in_buffer(struct cardstate *cs)
  1767. {
  1768. return cs->cmdbytes;
  1769. }
  1770. /* gigaset_brkchars
  1771. * implementation of ioctl(GIGASET_BRKCHARS)
  1772. * parameter:
  1773. * controller state structure
  1774. * return value:
  1775. * -EINVAL (unimplemented function)
  1776. */
  1777. static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
  1778. {
  1779. return -EINVAL;
  1780. }
  1781. /* Device Initialization/Shutdown */
  1782. /* ============================== */
  1783. /* Free hardware dependent part of the B channel structure
  1784. * parameter:
  1785. * bcs B channel structure
  1786. * return value:
  1787. * !=0 on success
  1788. */
  1789. static int gigaset_freebcshw(struct bc_state *bcs)
  1790. {
  1791. struct bas_bc_state *ubc = bcs->hw.bas;
  1792. int i;
  1793. if (!ubc)
  1794. return 0;
  1795. /* kill URBs and tasklets before freeing - better safe than sorry */
  1796. ubc->running = 0;
  1797. gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
  1798. for (i = 0; i < BAS_OUTURBS; ++i) {
  1799. usb_kill_urb(ubc->isoouturbs[i].urb);
  1800. usb_free_urb(ubc->isoouturbs[i].urb);
  1801. }
  1802. for (i = 0; i < BAS_INURBS; ++i) {
  1803. usb_kill_urb(ubc->isoinurbs[i]);
  1804. usb_free_urb(ubc->isoinurbs[i]);
  1805. }
  1806. tasklet_kill(&ubc->sent_tasklet);
  1807. tasklet_kill(&ubc->rcvd_tasklet);
  1808. kfree(ubc->isooutbuf);
  1809. kfree(ubc);
  1810. bcs->hw.bas = NULL;
  1811. return 1;
  1812. }
  1813. /* Initialize hardware dependent part of the B channel structure
  1814. * parameter:
  1815. * bcs B channel structure
  1816. * return value:
  1817. * !=0 on success
  1818. */
  1819. static int gigaset_initbcshw(struct bc_state *bcs)
  1820. {
  1821. int i;
  1822. struct bas_bc_state *ubc;
  1823. bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
  1824. if (!ubc) {
  1825. pr_err("out of memory\n");
  1826. return 0;
  1827. }
  1828. ubc->running = 0;
  1829. atomic_set(&ubc->corrbytes, 0);
  1830. spin_lock_init(&ubc->isooutlock);
  1831. for (i = 0; i < BAS_OUTURBS; ++i) {
  1832. ubc->isoouturbs[i].urb = NULL;
  1833. ubc->isoouturbs[i].bcs = bcs;
  1834. }
  1835. ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
  1836. ubc->numsub = 0;
  1837. ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
  1838. if (!ubc->isooutbuf) {
  1839. pr_err("out of memory\n");
  1840. kfree(ubc);
  1841. bcs->hw.bas = NULL;
  1842. return 0;
  1843. }
  1844. tasklet_init(&ubc->sent_tasklet,
  1845. write_iso_tasklet, (unsigned long) bcs);
  1846. spin_lock_init(&ubc->isoinlock);
  1847. for (i = 0; i < BAS_INURBS; ++i)
  1848. ubc->isoinurbs[i] = NULL;
  1849. ubc->isoindone = NULL;
  1850. ubc->loststatus = -EINPROGRESS;
  1851. ubc->isoinlost = 0;
  1852. ubc->seqlen = 0;
  1853. ubc->inbyte = 0;
  1854. ubc->inbits = 0;
  1855. ubc->goodbytes = 0;
  1856. ubc->alignerrs = 0;
  1857. ubc->fcserrs = 0;
  1858. ubc->frameerrs = 0;
  1859. ubc->giants = 0;
  1860. ubc->runts = 0;
  1861. ubc->aborts = 0;
  1862. ubc->shared0s = 0;
  1863. ubc->stolen0s = 0;
  1864. tasklet_init(&ubc->rcvd_tasklet,
  1865. read_iso_tasklet, (unsigned long) bcs);
  1866. return 1;
  1867. }
  1868. static void gigaset_reinitbcshw(struct bc_state *bcs)
  1869. {
  1870. struct bas_bc_state *ubc = bcs->hw.bas;
  1871. bcs->hw.bas->running = 0;
  1872. atomic_set(&bcs->hw.bas->corrbytes, 0);
  1873. bcs->hw.bas->numsub = 0;
  1874. spin_lock_init(&ubc->isooutlock);
  1875. spin_lock_init(&ubc->isoinlock);
  1876. ubc->loststatus = -EINPROGRESS;
  1877. }
  1878. static void gigaset_freecshw(struct cardstate *cs)
  1879. {
  1880. /* timers, URBs and rcvbuf are disposed of in disconnect */
  1881. kfree(cs->hw.bas->int_in_buf);
  1882. kfree(cs->hw.bas);
  1883. cs->hw.bas = NULL;
  1884. }
  1885. static int gigaset_initcshw(struct cardstate *cs)
  1886. {
  1887. struct bas_cardstate *ucs;
  1888. cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
  1889. if (!ucs) {
  1890. pr_err("out of memory\n");
  1891. return 0;
  1892. }
  1893. ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
  1894. if (!ucs->int_in_buf) {
  1895. kfree(ucs);
  1896. pr_err("out of memory\n");
  1897. return 0;
  1898. }
  1899. ucs->urb_cmd_in = NULL;
  1900. ucs->urb_cmd_out = NULL;
  1901. ucs->rcvbuf = NULL;
  1902. ucs->rcvbuf_size = 0;
  1903. spin_lock_init(&ucs->lock);
  1904. ucs->pending = 0;
  1905. ucs->basstate = 0;
  1906. setup_timer(&ucs->timer_ctrl, req_timeout, (unsigned long) cs);
  1907. setup_timer(&ucs->timer_atrdy, atrdy_timeout, (unsigned long) cs);
  1908. setup_timer(&ucs->timer_cmd_in, cmd_in_timeout, (unsigned long) cs);
  1909. init_waitqueue_head(&ucs->waitqueue);
  1910. return 1;
  1911. }
  1912. /* freeurbs
  1913. * unlink and deallocate all URBs unconditionally
  1914. * caller must make sure that no commands are still in progress
  1915. * parameter:
  1916. * cs controller state structure
  1917. */
  1918. static void freeurbs(struct cardstate *cs)
  1919. {
  1920. struct bas_cardstate *ucs = cs->hw.bas;
  1921. struct bas_bc_state *ubc;
  1922. int i, j;
  1923. gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
  1924. for (j = 0; j < BAS_CHANNELS; ++j) {
  1925. ubc = cs->bcs[j].hw.bas;
  1926. for (i = 0; i < BAS_OUTURBS; ++i) {
  1927. usb_kill_urb(ubc->isoouturbs[i].urb);
  1928. usb_free_urb(ubc->isoouturbs[i].urb);
  1929. ubc->isoouturbs[i].urb = NULL;
  1930. }
  1931. for (i = 0; i < BAS_INURBS; ++i) {
  1932. usb_kill_urb(ubc->isoinurbs[i]);
  1933. usb_free_urb(ubc->isoinurbs[i]);
  1934. ubc->isoinurbs[i] = NULL;
  1935. }
  1936. }
  1937. usb_kill_urb(ucs->urb_int_in);
  1938. usb_free_urb(ucs->urb_int_in);
  1939. ucs->urb_int_in = NULL;
  1940. usb_kill_urb(ucs->urb_cmd_out);
  1941. usb_free_urb(ucs->urb_cmd_out);
  1942. ucs->urb_cmd_out = NULL;
  1943. usb_kill_urb(ucs->urb_cmd_in);
  1944. usb_free_urb(ucs->urb_cmd_in);
  1945. ucs->urb_cmd_in = NULL;
  1946. usb_kill_urb(ucs->urb_ctrl);
  1947. usb_free_urb(ucs->urb_ctrl);
  1948. ucs->urb_ctrl = NULL;
  1949. }
  1950. /* gigaset_probe
  1951. * This function is called when a new USB device is connected.
  1952. * It checks whether the new device is handled by this driver.
  1953. */
  1954. static int gigaset_probe(struct usb_interface *interface,
  1955. const struct usb_device_id *id)
  1956. {
  1957. struct usb_host_interface *hostif;
  1958. struct usb_device *udev = interface_to_usbdev(interface);
  1959. struct cardstate *cs = NULL;
  1960. struct bas_cardstate *ucs = NULL;
  1961. struct bas_bc_state *ubc;
  1962. struct usb_endpoint_descriptor *endpoint;
  1963. int i, j;
  1964. int rc;
  1965. gig_dbg(DEBUG_INIT,
  1966. "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
  1967. __func__, le16_to_cpu(udev->descriptor.idVendor),
  1968. le16_to_cpu(udev->descriptor.idProduct));
  1969. /* set required alternate setting */
  1970. hostif = interface->cur_altsetting;
  1971. if (hostif->desc.bAlternateSetting != 3) {
  1972. gig_dbg(DEBUG_INIT,
  1973. "%s: wrong alternate setting %d - trying to switch",
  1974. __func__, hostif->desc.bAlternateSetting);
  1975. if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
  1976. < 0) {
  1977. dev_warn(&udev->dev, "usb_set_interface failed, "
  1978. "device %d interface %d altsetting %d\n",
  1979. udev->devnum, hostif->desc.bInterfaceNumber,
  1980. hostif->desc.bAlternateSetting);
  1981. return -ENODEV;
  1982. }
  1983. hostif = interface->cur_altsetting;
  1984. }
  1985. /* Reject application specific interfaces
  1986. */
  1987. if (hostif->desc.bInterfaceClass != 255) {
  1988. dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
  1989. __func__, hostif->desc.bInterfaceClass);
  1990. return -ENODEV;
  1991. }
  1992. dev_info(&udev->dev,
  1993. "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
  1994. __func__, le16_to_cpu(udev->descriptor.idVendor),
  1995. le16_to_cpu(udev->descriptor.idProduct));
  1996. /* allocate memory for our device state and intialize it */
  1997. cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
  1998. GIGASET_MODULENAME);
  1999. if (!cs)
  2000. return -ENODEV;
  2001. ucs = cs->hw.bas;
  2002. /* save off device structure ptrs for later use */
  2003. usb_get_dev(udev);
  2004. ucs->udev = udev;
  2005. ucs->interface = interface;
  2006. cs->dev = &interface->dev;
  2007. /* allocate URBs:
  2008. * - one for the interrupt pipe
  2009. * - three for the different uses of the default control pipe
  2010. * - three for each isochronous pipe
  2011. */
  2012. if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
  2013. !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
  2014. !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
  2015. !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
  2016. goto allocerr;
  2017. for (j = 0; j < BAS_CHANNELS; ++j) {
  2018. ubc = cs->bcs[j].hw.bas;
  2019. for (i = 0; i < BAS_OUTURBS; ++i)
  2020. if (!(ubc->isoouturbs[i].urb =
  2021. usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
  2022. goto allocerr;
  2023. for (i = 0; i < BAS_INURBS; ++i)
  2024. if (!(ubc->isoinurbs[i] =
  2025. usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
  2026. goto allocerr;
  2027. }
  2028. ucs->rcvbuf = NULL;
  2029. ucs->rcvbuf_size = 0;
  2030. /* Fill the interrupt urb and send it to the core */
  2031. endpoint = &hostif->endpoint[0].desc;
  2032. usb_fill_int_urb(ucs->urb_int_in, udev,
  2033. usb_rcvintpipe(udev,
  2034. (endpoint->bEndpointAddress) & 0x0f),
  2035. ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
  2036. endpoint->bInterval);
  2037. rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
  2038. if (rc != 0) {
  2039. dev_err(cs->dev, "could not submit interrupt URB: %s\n",
  2040. get_usb_rcmsg(rc));
  2041. goto error;
  2042. }
  2043. /* tell the device that the driver is ready */
  2044. rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
  2045. if (rc != 0)
  2046. goto error;
  2047. /* tell common part that the device is ready */
  2048. if (startmode == SM_LOCKED)
  2049. cs->mstate = MS_LOCKED;
  2050. /* save address of controller structure */
  2051. usb_set_intfdata(interface, cs);
  2052. if (!gigaset_start(cs))
  2053. goto error;
  2054. return 0;
  2055. allocerr:
  2056. dev_err(cs->dev, "could not allocate URBs\n");
  2057. error:
  2058. freeurbs(cs);
  2059. usb_set_intfdata(interface, NULL);
  2060. gigaset_freecs(cs);
  2061. return -ENODEV;
  2062. }
  2063. /* gigaset_disconnect
  2064. * This function is called when the Gigaset base is unplugged.
  2065. */
  2066. static void gigaset_disconnect(struct usb_interface *interface)
  2067. {
  2068. struct cardstate *cs;
  2069. struct bas_cardstate *ucs;
  2070. int j;
  2071. cs = usb_get_intfdata(interface);
  2072. ucs = cs->hw.bas;
  2073. dev_info(cs->dev, "disconnecting Gigaset base\n");
  2074. /* mark base as not ready, all channels disconnected */
  2075. ucs->basstate = 0;
  2076. /* tell LL all channels are down */
  2077. for (j = 0; j < BAS_CHANNELS; ++j)
  2078. gigaset_bchannel_down(cs->bcs + j);
  2079. /* stop driver (common part) */
  2080. gigaset_stop(cs);
  2081. /* stop timers and URBs, free ressources */
  2082. del_timer_sync(&ucs->timer_ctrl);
  2083. del_timer_sync(&ucs->timer_atrdy);
  2084. del_timer_sync(&ucs->timer_cmd_in);
  2085. freeurbs(cs);
  2086. usb_set_intfdata(interface, NULL);
  2087. kfree(ucs->rcvbuf);
  2088. ucs->rcvbuf = NULL;
  2089. ucs->rcvbuf_size = 0;
  2090. usb_put_dev(ucs->udev);
  2091. ucs->interface = NULL;
  2092. ucs->udev = NULL;
  2093. cs->dev = NULL;
  2094. gigaset_freecs(cs);
  2095. }
  2096. /* gigaset_suspend
  2097. * This function is called before the USB connection is suspended.
  2098. */
  2099. static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
  2100. {
  2101. struct cardstate *cs = usb_get_intfdata(intf);
  2102. struct bas_cardstate *ucs = cs->hw.bas;
  2103. int rc;
  2104. /* set suspend flag; this stops AT command/response traffic */
  2105. if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
  2106. gig_dbg(DEBUG_SUSPEND, "already suspended");
  2107. return 0;
  2108. }
  2109. /* wait a bit for blocking conditions to go away */
  2110. rc = wait_event_timeout(ucs->waitqueue,
  2111. !(ucs->basstate &
  2112. (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
  2113. BAS_TIMEOUT*HZ/10);
  2114. gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
  2115. /* check for conditions preventing suspend */
  2116. if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
  2117. dev_warn(cs->dev, "cannot suspend:\n");
  2118. if (ucs->basstate & BS_B1OPEN)
  2119. dev_warn(cs->dev, " B channel 1 open\n");
  2120. if (ucs->basstate & BS_B2OPEN)
  2121. dev_warn(cs->dev, " B channel 2 open\n");
  2122. if (ucs->basstate & BS_ATRDPEND)
  2123. dev_warn(cs->dev, " receiving AT reply\n");
  2124. if (ucs->basstate & BS_ATWRPEND)
  2125. dev_warn(cs->dev, " sending AT command\n");
  2126. update_basstate(ucs, 0, BS_SUSPEND);
  2127. return -EBUSY;
  2128. }
  2129. /* close AT channel if open */
  2130. if (ucs->basstate & BS_ATOPEN) {
  2131. gig_dbg(DEBUG_SUSPEND, "closing AT channel");
  2132. rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
  2133. if (rc) {
  2134. update_basstate(ucs, 0, BS_SUSPEND);
  2135. return rc;
  2136. }
  2137. wait_event_timeout(ucs->waitqueue, !ucs->pending,
  2138. BAS_TIMEOUT*HZ/10);
  2139. /* in case of timeout, proceed anyway */
  2140. }
  2141. /* kill all URBs and timers that might still be pending */
  2142. usb_kill_urb(ucs->urb_ctrl);
  2143. usb_kill_urb(ucs->urb_int_in);
  2144. del_timer_sync(&ucs->timer_ctrl);
  2145. del_timer_sync(&ucs->timer_atrdy);
  2146. del_timer_sync(&ucs->timer_cmd_in);
  2147. gig_dbg(DEBUG_SUSPEND, "suspend complete");
  2148. return 0;
  2149. }
  2150. /* gigaset_resume
  2151. * This function is called after the USB connection has been resumed.
  2152. */
  2153. static int gigaset_resume(struct usb_interface *intf)
  2154. {
  2155. struct cardstate *cs = usb_get_intfdata(intf);
  2156. struct bas_cardstate *ucs = cs->hw.bas;
  2157. int rc;
  2158. /* resubmit interrupt URB for spontaneous messages from base */
  2159. rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
  2160. if (rc) {
  2161. dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
  2162. get_usb_rcmsg(rc));
  2163. return rc;
  2164. }
  2165. /* clear suspend flag to reallow activity */
  2166. update_basstate(ucs, 0, BS_SUSPEND);
  2167. gig_dbg(DEBUG_SUSPEND, "resume complete");
  2168. return 0;
  2169. }
  2170. /* gigaset_pre_reset
  2171. * This function is called before the USB connection is reset.
  2172. */
  2173. static int gigaset_pre_reset(struct usb_interface *intf)
  2174. {
  2175. /* handle just like suspend */
  2176. return gigaset_suspend(intf, PMSG_ON);
  2177. }
  2178. /* gigaset_post_reset
  2179. * This function is called after the USB connection has been reset.
  2180. */
  2181. static int gigaset_post_reset(struct usb_interface *intf)
  2182. {
  2183. /* FIXME: send HD_DEVICE_INIT_ACK? */
  2184. /* resume operations */
  2185. return gigaset_resume(intf);
  2186. }
  2187. static const struct gigaset_ops gigops = {
  2188. gigaset_write_cmd,
  2189. gigaset_write_room,
  2190. gigaset_chars_in_buffer,
  2191. gigaset_brkchars,
  2192. gigaset_init_bchannel,
  2193. gigaset_close_bchannel,
  2194. gigaset_initbcshw,
  2195. gigaset_freebcshw,
  2196. gigaset_reinitbcshw,
  2197. gigaset_initcshw,
  2198. gigaset_freecshw,
  2199. gigaset_set_modem_ctrl,
  2200. gigaset_baud_rate,
  2201. gigaset_set_line_ctrl,
  2202. gigaset_isoc_send_skb,
  2203. gigaset_isoc_input,
  2204. };
  2205. /* bas_gigaset_init
  2206. * This function is called after the kernel module is loaded.
  2207. */
  2208. static int __init bas_gigaset_init(void)
  2209. {
  2210. int result;
  2211. /* allocate memory for our driver state and intialize it */
  2212. driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
  2213. GIGASET_MODULENAME, GIGASET_DEVNAME,
  2214. &gigops, THIS_MODULE);
  2215. if (driver == NULL)
  2216. goto error;
  2217. /* register this driver with the USB subsystem */
  2218. result = usb_register(&gigaset_usb_driver);
  2219. if (result < 0) {
  2220. pr_err("error %d registering USB driver\n", -result);
  2221. goto error;
  2222. }
  2223. pr_info(DRIVER_DESC "\n");
  2224. return 0;
  2225. error:
  2226. if (driver)
  2227. gigaset_freedriver(driver);
  2228. driver = NULL;
  2229. return -1;
  2230. }
  2231. /* bas_gigaset_exit
  2232. * This function is called before the kernel module is unloaded.
  2233. */
  2234. static void __exit bas_gigaset_exit(void)
  2235. {
  2236. struct bas_cardstate *ucs;
  2237. int i;
  2238. gigaset_blockdriver(driver); /* => probe will fail
  2239. * => no gigaset_start any more
  2240. */
  2241. /* stop all connected devices */
  2242. for (i = 0; i < driver->minors; i++) {
  2243. if (gigaset_shutdown(driver->cs + i) < 0)
  2244. continue; /* no device */
  2245. /* from now on, no isdn callback should be possible */
  2246. /* close all still open channels */
  2247. ucs = driver->cs[i].hw.bas;
  2248. if (ucs->basstate & BS_B1OPEN) {
  2249. gig_dbg(DEBUG_INIT, "closing B1 channel");
  2250. usb_control_msg(ucs->udev,
  2251. usb_sndctrlpipe(ucs->udev, 0),
  2252. HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
  2253. 0, 0, NULL, 0, BAS_TIMEOUT);
  2254. }
  2255. if (ucs->basstate & BS_B2OPEN) {
  2256. gig_dbg(DEBUG_INIT, "closing B2 channel");
  2257. usb_control_msg(ucs->udev,
  2258. usb_sndctrlpipe(ucs->udev, 0),
  2259. HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
  2260. 0, 0, NULL, 0, BAS_TIMEOUT);
  2261. }
  2262. if (ucs->basstate & BS_ATOPEN) {
  2263. gig_dbg(DEBUG_INIT, "closing AT channel");
  2264. usb_control_msg(ucs->udev,
  2265. usb_sndctrlpipe(ucs->udev, 0),
  2266. HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
  2267. 0, 0, NULL, 0, BAS_TIMEOUT);
  2268. }
  2269. ucs->basstate = 0;
  2270. }
  2271. /* deregister this driver with the USB subsystem */
  2272. usb_deregister(&gigaset_usb_driver);
  2273. /* this will call the disconnect-callback */
  2274. /* from now on, no disconnect/probe callback should be running */
  2275. gigaset_freedriver(driver);
  2276. driver = NULL;
  2277. }
  2278. module_init(bas_gigaset_init);
  2279. module_exit(bas_gigaset_exit);
  2280. MODULE_AUTHOR(DRIVER_AUTHOR);
  2281. MODULE_DESCRIPTION(DRIVER_DESC);
  2282. MODULE_LICENSE("GPL");