bas-gigaset.c 69 KB

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