bas-gigaset.c 63 KB

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