bas-gigaset.c 67 KB

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