bas-gigaset.c 68 KB

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