bas-gigaset.c 67 KB

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