bas-gigaset.c 64 KB

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