bas-gigaset.c 69 KB

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