bas-gigaset.c 65 KB

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