bas-gigaset.c 71 KB

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