bas-gigaset.c 71 KB

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