bas-gigaset.c 69 KB

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