bas-gigaset.c 63 KB

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