bas-gigaset.c 64 KB

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