bas-gigaset.c 69 KB

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