hso.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Filip Aben <f.aben@option.com>
  7. * Denis Joseph Barrow <d.barow@option.com>
  8. * Jan Dumon <j.dumon@option.com>
  9. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  10. * <ajb@spheresystems.co.uk>
  11. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  12. * Copyright (C) 2008 Novell, Inc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  26. * USA
  27. *
  28. *
  29. *****************************************************************************/
  30. /******************************************************************************
  31. *
  32. * Description of the device:
  33. *
  34. * Interface 0: Contains the IP network interface on the bulk end points.
  35. * The multiplexed serial ports are using the interrupt and
  36. * control endpoints.
  37. * Interrupt contains a bitmap telling which multiplexed
  38. * serialport needs servicing.
  39. *
  40. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  41. * port is opened, as this have a huge impact on the network port
  42. * throughput.
  43. *
  44. * Interface 2: Standard modem interface - circuit switched interface, this
  45. * can be used to make a standard ppp connection however it
  46. * should not be used in conjunction with the IP network interface
  47. * enabled for USB performance reasons i.e. if using this set
  48. * ideally disable_net=1.
  49. *
  50. *****************************************************************************/
  51. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  52. #include <linux/sched.h>
  53. #include <linux/slab.h>
  54. #include <linux/init.h>
  55. #include <linux/delay.h>
  56. #include <linux/netdevice.h>
  57. #include <linux/module.h>
  58. #include <linux/ethtool.h>
  59. #include <linux/usb.h>
  60. #include <linux/tty.h>
  61. #include <linux/tty_driver.h>
  62. #include <linux/tty_flip.h>
  63. #include <linux/kmod.h>
  64. #include <linux/rfkill.h>
  65. #include <linux/ip.h>
  66. #include <linux/uaccess.h>
  67. #include <linux/usb/cdc.h>
  68. #include <net/arp.h>
  69. #include <asm/byteorder.h>
  70. #include <linux/serial_core.h>
  71. #include <linux/serial.h>
  72. #define MOD_AUTHOR "Option Wireless"
  73. #define MOD_DESCRIPTION "USB High Speed Option driver"
  74. #define MOD_LICENSE "GPL"
  75. #define HSO_MAX_NET_DEVICES 10
  76. #define HSO__MAX_MTU 2048
  77. #define DEFAULT_MTU 1500
  78. #define DEFAULT_MRU 1500
  79. #define CTRL_URB_RX_SIZE 1024
  80. #define CTRL_URB_TX_SIZE 64
  81. #define BULK_URB_RX_SIZE 4096
  82. #define BULK_URB_TX_SIZE 8192
  83. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  84. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  85. #define MUX_BULK_RX_BUF_COUNT 4
  86. #define USB_TYPE_OPTION_VENDOR 0x20
  87. /* These definitions are used with the struct hso_net flags element */
  88. /* - use *_bit operations on it. (bit indices not values.) */
  89. #define HSO_NET_RUNNING 0
  90. #define HSO_NET_TX_TIMEOUT (HZ*10)
  91. #define HSO_SERIAL_MAGIC 0x48534f31
  92. /* Number of ttys to handle */
  93. #define HSO_SERIAL_TTY_MINORS 256
  94. #define MAX_RX_URBS 2
  95. /*****************************************************************************/
  96. /* Debugging functions */
  97. /*****************************************************************************/
  98. #define hso_dbg(lvl, fmt, ...) \
  99. do { \
  100. if ((lvl) & debug) \
  101. pr_info("[%d:%s] " fmt, \
  102. __LINE__, __func__, ##__VA_ARGS__); \
  103. } while (0)
  104. /*****************************************************************************/
  105. /* Enumerators */
  106. /*****************************************************************************/
  107. enum pkt_parse_state {
  108. WAIT_IP,
  109. WAIT_DATA,
  110. WAIT_SYNC
  111. };
  112. /*****************************************************************************/
  113. /* Structs */
  114. /*****************************************************************************/
  115. struct hso_shared_int {
  116. struct usb_endpoint_descriptor *intr_endp;
  117. void *shared_intr_buf;
  118. struct urb *shared_intr_urb;
  119. struct usb_device *usb;
  120. int use_count;
  121. int ref_count;
  122. struct mutex shared_int_lock;
  123. };
  124. struct hso_net {
  125. struct hso_device *parent;
  126. struct net_device *net;
  127. struct rfkill *rfkill;
  128. char name[24];
  129. struct usb_endpoint_descriptor *in_endp;
  130. struct usb_endpoint_descriptor *out_endp;
  131. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  132. struct urb *mux_bulk_tx_urb;
  133. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  134. void *mux_bulk_tx_buf;
  135. struct sk_buff *skb_rx_buf;
  136. struct sk_buff *skb_tx_buf;
  137. enum pkt_parse_state rx_parse_state;
  138. spinlock_t net_lock;
  139. unsigned short rx_buf_size;
  140. unsigned short rx_buf_missing;
  141. struct iphdr rx_ip_hdr;
  142. unsigned long flags;
  143. };
  144. enum rx_ctrl_state{
  145. RX_IDLE,
  146. RX_SENT,
  147. RX_PENDING
  148. };
  149. #define BM_REQUEST_TYPE (0xa1)
  150. #define B_NOTIFICATION (0x20)
  151. #define W_VALUE (0x0)
  152. #define W_LENGTH (0x2)
  153. #define B_OVERRUN (0x1<<6)
  154. #define B_PARITY (0x1<<5)
  155. #define B_FRAMING (0x1<<4)
  156. #define B_RING_SIGNAL (0x1<<3)
  157. #define B_BREAK (0x1<<2)
  158. #define B_TX_CARRIER (0x1<<1)
  159. #define B_RX_CARRIER (0x1<<0)
  160. struct hso_serial_state_notification {
  161. u8 bmRequestType;
  162. u8 bNotification;
  163. u16 wValue;
  164. u16 wIndex;
  165. u16 wLength;
  166. u16 UART_state_bitmap;
  167. } __packed;
  168. struct hso_tiocmget {
  169. struct mutex mutex;
  170. wait_queue_head_t waitq;
  171. int intr_completed;
  172. struct usb_endpoint_descriptor *endp;
  173. struct urb *urb;
  174. struct hso_serial_state_notification serial_state_notification;
  175. u16 prev_UART_state_bitmap;
  176. struct uart_icount icount;
  177. };
  178. struct hso_serial {
  179. struct hso_device *parent;
  180. int magic;
  181. u8 minor;
  182. struct hso_shared_int *shared_int;
  183. /* rx/tx urb could be either a bulk urb or a control urb depending
  184. on which serial port it is used on. */
  185. struct urb *rx_urb[MAX_RX_URBS];
  186. u8 num_rx_urbs;
  187. u8 *rx_data[MAX_RX_URBS];
  188. u16 rx_data_length; /* should contain allocated length */
  189. struct urb *tx_urb;
  190. u8 *tx_data;
  191. u8 *tx_buffer;
  192. u16 tx_data_length; /* should contain allocated length */
  193. u16 tx_data_count;
  194. u16 tx_buffer_count;
  195. struct usb_ctrlrequest ctrl_req_tx;
  196. struct usb_ctrlrequest ctrl_req_rx;
  197. struct usb_endpoint_descriptor *in_endp;
  198. struct usb_endpoint_descriptor *out_endp;
  199. enum rx_ctrl_state rx_state;
  200. u8 rts_state;
  201. u8 dtr_state;
  202. unsigned tx_urb_used:1;
  203. struct tty_port port;
  204. /* from usb_serial_port */
  205. spinlock_t serial_lock;
  206. int (*write_data) (struct hso_serial *serial);
  207. struct hso_tiocmget *tiocmget;
  208. /* Hacks required to get flow control
  209. * working on the serial receive buffers
  210. * so as not to drop characters on the floor.
  211. */
  212. int curr_rx_urb_idx;
  213. u8 rx_urb_filled[MAX_RX_URBS];
  214. struct tasklet_struct unthrottle_tasklet;
  215. };
  216. struct hso_device {
  217. union {
  218. struct hso_serial *dev_serial;
  219. struct hso_net *dev_net;
  220. } port_data;
  221. u32 port_spec;
  222. u8 is_active;
  223. u8 usb_gone;
  224. struct work_struct async_get_intf;
  225. struct work_struct async_put_intf;
  226. struct usb_device *usb;
  227. struct usb_interface *interface;
  228. struct device *dev;
  229. struct kref ref;
  230. struct mutex mutex;
  231. };
  232. /* Type of interface */
  233. #define HSO_INTF_MASK 0xFF00
  234. #define HSO_INTF_MUX 0x0100
  235. #define HSO_INTF_BULK 0x0200
  236. /* Type of port */
  237. #define HSO_PORT_MASK 0xFF
  238. #define HSO_PORT_NO_PORT 0x0
  239. #define HSO_PORT_CONTROL 0x1
  240. #define HSO_PORT_APP 0x2
  241. #define HSO_PORT_GPS 0x3
  242. #define HSO_PORT_PCSC 0x4
  243. #define HSO_PORT_APP2 0x5
  244. #define HSO_PORT_GPS_CONTROL 0x6
  245. #define HSO_PORT_MSD 0x7
  246. #define HSO_PORT_VOICE 0x8
  247. #define HSO_PORT_DIAG2 0x9
  248. #define HSO_PORT_DIAG 0x10
  249. #define HSO_PORT_MODEM 0x11
  250. #define HSO_PORT_NETWORK 0x12
  251. /* Additional device info */
  252. #define HSO_INFO_MASK 0xFF000000
  253. #define HSO_INFO_CRC_BUG 0x01000000
  254. /*****************************************************************************/
  255. /* Prototypes */
  256. /*****************************************************************************/
  257. /* Serial driver functions */
  258. static int hso_serial_tiocmset(struct tty_struct *tty,
  259. unsigned int set, unsigned int clear);
  260. static void ctrl_callback(struct urb *urb);
  261. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  262. static void hso_kick_transmit(struct hso_serial *serial);
  263. /* Helper functions */
  264. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  265. struct usb_device *usb, gfp_t gfp);
  266. static void handle_usb_error(int status, const char *function,
  267. struct hso_device *hso_dev);
  268. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  269. int type, int dir);
  270. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  271. static void hso_free_interface(struct usb_interface *intf);
  272. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  273. static int hso_stop_serial_device(struct hso_device *hso_dev);
  274. static int hso_start_net_device(struct hso_device *hso_dev);
  275. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  276. static int hso_stop_net_device(struct hso_device *hso_dev);
  277. static void hso_serial_ref_free(struct kref *ref);
  278. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  279. static int hso_mux_serial_read(struct hso_serial *serial);
  280. static void async_get_intf(struct work_struct *data);
  281. static void async_put_intf(struct work_struct *data);
  282. static int hso_put_activity(struct hso_device *hso_dev);
  283. static int hso_get_activity(struct hso_device *hso_dev);
  284. static void tiocmget_intr_callback(struct urb *urb);
  285. /*****************************************************************************/
  286. /* Helping functions */
  287. /*****************************************************************************/
  288. /* #define DEBUG */
  289. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  290. {
  291. return hso_dev->port_data.dev_net;
  292. }
  293. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  294. {
  295. return hso_dev->port_data.dev_serial;
  296. }
  297. /* Debugging functions */
  298. #ifdef DEBUG
  299. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  300. unsigned int len)
  301. {
  302. static char name[255];
  303. sprintf(name, "hso[%d:%s]", line_count, func_name);
  304. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  305. }
  306. #define DUMP(buf_, len_) \
  307. dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
  308. #define DUMP1(buf_, len_) \
  309. do { \
  310. if (0x01 & debug) \
  311. DUMP(buf_, len_); \
  312. } while (0)
  313. #else
  314. #define DUMP(buf_, len_)
  315. #define DUMP1(buf_, len_)
  316. #endif
  317. /* module parameters */
  318. static int debug;
  319. static int tty_major;
  320. static int disable_net;
  321. /* driver info */
  322. static const char driver_name[] = "hso";
  323. static const char tty_filename[] = "ttyHS";
  324. static const char *version = __FILE__ ": " MOD_AUTHOR;
  325. /* the usb driver itself (registered in hso_init) */
  326. static struct usb_driver hso_driver;
  327. /* serial structures */
  328. static struct tty_driver *tty_drv;
  329. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  330. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  331. static spinlock_t serial_table_lock;
  332. static const s32 default_port_spec[] = {
  333. HSO_INTF_MUX | HSO_PORT_NETWORK,
  334. HSO_INTF_BULK | HSO_PORT_DIAG,
  335. HSO_INTF_BULK | HSO_PORT_MODEM,
  336. 0
  337. };
  338. static const s32 icon321_port_spec[] = {
  339. HSO_INTF_MUX | HSO_PORT_NETWORK,
  340. HSO_INTF_BULK | HSO_PORT_DIAG2,
  341. HSO_INTF_BULK | HSO_PORT_MODEM,
  342. HSO_INTF_BULK | HSO_PORT_DIAG,
  343. 0
  344. };
  345. #define default_port_device(vendor, product) \
  346. USB_DEVICE(vendor, product), \
  347. .driver_info = (kernel_ulong_t)default_port_spec
  348. #define icon321_port_device(vendor, product) \
  349. USB_DEVICE(vendor, product), \
  350. .driver_info = (kernel_ulong_t)icon321_port_spec
  351. /* list of devices we support */
  352. static const struct usb_device_id hso_ids[] = {
  353. {default_port_device(0x0af0, 0x6711)},
  354. {default_port_device(0x0af0, 0x6731)},
  355. {default_port_device(0x0af0, 0x6751)},
  356. {default_port_device(0x0af0, 0x6771)},
  357. {default_port_device(0x0af0, 0x6791)},
  358. {default_port_device(0x0af0, 0x6811)},
  359. {default_port_device(0x0af0, 0x6911)},
  360. {default_port_device(0x0af0, 0x6951)},
  361. {default_port_device(0x0af0, 0x6971)},
  362. {default_port_device(0x0af0, 0x7011)},
  363. {default_port_device(0x0af0, 0x7031)},
  364. {default_port_device(0x0af0, 0x7051)},
  365. {default_port_device(0x0af0, 0x7071)},
  366. {default_port_device(0x0af0, 0x7111)},
  367. {default_port_device(0x0af0, 0x7211)},
  368. {default_port_device(0x0af0, 0x7251)},
  369. {default_port_device(0x0af0, 0x7271)},
  370. {default_port_device(0x0af0, 0x7311)},
  371. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  372. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  373. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  374. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  375. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  376. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  377. {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
  378. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  379. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  380. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  381. {USB_DEVICE(0x0af0, 0x7701)},
  382. {USB_DEVICE(0x0af0, 0x7706)},
  383. {USB_DEVICE(0x0af0, 0x7801)},
  384. {USB_DEVICE(0x0af0, 0x7901)},
  385. {USB_DEVICE(0x0af0, 0x7A01)},
  386. {USB_DEVICE(0x0af0, 0x7A05)},
  387. {USB_DEVICE(0x0af0, 0x8200)},
  388. {USB_DEVICE(0x0af0, 0x8201)},
  389. {USB_DEVICE(0x0af0, 0x8300)},
  390. {USB_DEVICE(0x0af0, 0x8302)},
  391. {USB_DEVICE(0x0af0, 0x8304)},
  392. {USB_DEVICE(0x0af0, 0x8400)},
  393. {USB_DEVICE(0x0af0, 0x8600)},
  394. {USB_DEVICE(0x0af0, 0x8800)},
  395. {USB_DEVICE(0x0af0, 0x8900)},
  396. {USB_DEVICE(0x0af0, 0x9000)},
  397. {USB_DEVICE(0x0af0, 0x9200)}, /* Option GTM671WFS */
  398. {USB_DEVICE(0x0af0, 0xd035)},
  399. {USB_DEVICE(0x0af0, 0xd055)},
  400. {USB_DEVICE(0x0af0, 0xd155)},
  401. {USB_DEVICE(0x0af0, 0xd255)},
  402. {USB_DEVICE(0x0af0, 0xd057)},
  403. {USB_DEVICE(0x0af0, 0xd157)},
  404. {USB_DEVICE(0x0af0, 0xd257)},
  405. {USB_DEVICE(0x0af0, 0xd357)},
  406. {USB_DEVICE(0x0af0, 0xd058)},
  407. {USB_DEVICE(0x0af0, 0xc100)},
  408. {}
  409. };
  410. MODULE_DEVICE_TABLE(usb, hso_ids);
  411. /* Sysfs attribute */
  412. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  413. struct device_attribute *attr,
  414. char *buf)
  415. {
  416. struct hso_device *hso_dev = dev_get_drvdata(dev);
  417. char *port_name;
  418. if (!hso_dev)
  419. return 0;
  420. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  421. case HSO_PORT_CONTROL:
  422. port_name = "Control";
  423. break;
  424. case HSO_PORT_APP:
  425. port_name = "Application";
  426. break;
  427. case HSO_PORT_APP2:
  428. port_name = "Application2";
  429. break;
  430. case HSO_PORT_GPS:
  431. port_name = "GPS";
  432. break;
  433. case HSO_PORT_GPS_CONTROL:
  434. port_name = "GPS Control";
  435. break;
  436. case HSO_PORT_PCSC:
  437. port_name = "PCSC";
  438. break;
  439. case HSO_PORT_DIAG:
  440. port_name = "Diagnostic";
  441. break;
  442. case HSO_PORT_DIAG2:
  443. port_name = "Diagnostic2";
  444. break;
  445. case HSO_PORT_MODEM:
  446. port_name = "Modem";
  447. break;
  448. case HSO_PORT_NETWORK:
  449. port_name = "Network";
  450. break;
  451. default:
  452. port_name = "Unknown";
  453. break;
  454. }
  455. return sprintf(buf, "%s\n", port_name);
  456. }
  457. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  458. static struct attribute *hso_serial_dev_attrs[] = {
  459. &dev_attr_hsotype.attr,
  460. NULL
  461. };
  462. ATTRIBUTE_GROUPS(hso_serial_dev);
  463. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  464. {
  465. int idx;
  466. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  467. if (serial->rx_urb[idx] == urb)
  468. return idx;
  469. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  470. return -1;
  471. }
  472. /* converts mux value to a port spec value */
  473. static u32 hso_mux_to_port(int mux)
  474. {
  475. u32 result;
  476. switch (mux) {
  477. case 0x1:
  478. result = HSO_PORT_CONTROL;
  479. break;
  480. case 0x2:
  481. result = HSO_PORT_APP;
  482. break;
  483. case 0x4:
  484. result = HSO_PORT_PCSC;
  485. break;
  486. case 0x8:
  487. result = HSO_PORT_GPS;
  488. break;
  489. case 0x10:
  490. result = HSO_PORT_APP2;
  491. break;
  492. default:
  493. result = HSO_PORT_NO_PORT;
  494. }
  495. return result;
  496. }
  497. /* converts port spec value to a mux value */
  498. static u32 hso_port_to_mux(int port)
  499. {
  500. u32 result;
  501. switch (port & HSO_PORT_MASK) {
  502. case HSO_PORT_CONTROL:
  503. result = 0x0;
  504. break;
  505. case HSO_PORT_APP:
  506. result = 0x1;
  507. break;
  508. case HSO_PORT_PCSC:
  509. result = 0x2;
  510. break;
  511. case HSO_PORT_GPS:
  512. result = 0x3;
  513. break;
  514. case HSO_PORT_APP2:
  515. result = 0x4;
  516. break;
  517. default:
  518. result = 0x0;
  519. }
  520. return result;
  521. }
  522. static struct hso_serial *get_serial_by_shared_int_and_type(
  523. struct hso_shared_int *shared_int,
  524. int mux)
  525. {
  526. int i, port;
  527. port = hso_mux_to_port(mux);
  528. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  529. if (serial_table[i] &&
  530. (dev2ser(serial_table[i])->shared_int == shared_int) &&
  531. ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  532. return dev2ser(serial_table[i]);
  533. }
  534. }
  535. return NULL;
  536. }
  537. static struct hso_serial *get_serial_by_index(unsigned index)
  538. {
  539. struct hso_serial *serial = NULL;
  540. unsigned long flags;
  541. spin_lock_irqsave(&serial_table_lock, flags);
  542. if (serial_table[index])
  543. serial = dev2ser(serial_table[index]);
  544. spin_unlock_irqrestore(&serial_table_lock, flags);
  545. return serial;
  546. }
  547. static int get_free_serial_index(void)
  548. {
  549. int index;
  550. unsigned long flags;
  551. spin_lock_irqsave(&serial_table_lock, flags);
  552. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  553. if (serial_table[index] == NULL) {
  554. spin_unlock_irqrestore(&serial_table_lock, flags);
  555. return index;
  556. }
  557. }
  558. spin_unlock_irqrestore(&serial_table_lock, flags);
  559. pr_err("%s: no free serial devices in table\n", __func__);
  560. return -1;
  561. }
  562. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  563. {
  564. unsigned long flags;
  565. spin_lock_irqsave(&serial_table_lock, flags);
  566. if (serial)
  567. serial_table[index] = serial->parent;
  568. else
  569. serial_table[index] = NULL;
  570. spin_unlock_irqrestore(&serial_table_lock, flags);
  571. }
  572. static void handle_usb_error(int status, const char *function,
  573. struct hso_device *hso_dev)
  574. {
  575. char *explanation;
  576. switch (status) {
  577. case -ENODEV:
  578. explanation = "no device";
  579. break;
  580. case -ENOENT:
  581. explanation = "endpoint not enabled";
  582. break;
  583. case -EPIPE:
  584. explanation = "endpoint stalled";
  585. break;
  586. case -ENOSPC:
  587. explanation = "not enough bandwidth";
  588. break;
  589. case -ESHUTDOWN:
  590. explanation = "device disabled";
  591. break;
  592. case -EHOSTUNREACH:
  593. explanation = "device suspended";
  594. break;
  595. case -EINVAL:
  596. case -EAGAIN:
  597. case -EFBIG:
  598. case -EMSGSIZE:
  599. explanation = "internal error";
  600. break;
  601. case -EILSEQ:
  602. case -EPROTO:
  603. case -ETIME:
  604. case -ETIMEDOUT:
  605. explanation = "protocol error";
  606. if (hso_dev)
  607. usb_queue_reset_device(hso_dev->interface);
  608. break;
  609. default:
  610. explanation = "unknown status";
  611. break;
  612. }
  613. /* log a meaningful explanation of an USB status */
  614. hso_dbg(0x1, "%s: received USB status - %s (%d)\n",
  615. function, explanation, status);
  616. }
  617. /* Network interface functions */
  618. /* called when net interface is brought up by ifconfig */
  619. static int hso_net_open(struct net_device *net)
  620. {
  621. struct hso_net *odev = netdev_priv(net);
  622. unsigned long flags = 0;
  623. if (!odev) {
  624. dev_err(&net->dev, "No net device !\n");
  625. return -ENODEV;
  626. }
  627. odev->skb_tx_buf = NULL;
  628. /* setup environment */
  629. spin_lock_irqsave(&odev->net_lock, flags);
  630. odev->rx_parse_state = WAIT_IP;
  631. odev->rx_buf_size = 0;
  632. odev->rx_buf_missing = sizeof(struct iphdr);
  633. spin_unlock_irqrestore(&odev->net_lock, flags);
  634. /* We are up and running. */
  635. set_bit(HSO_NET_RUNNING, &odev->flags);
  636. hso_start_net_device(odev->parent);
  637. /* Tell the kernel we are ready to start receiving from it */
  638. netif_start_queue(net);
  639. return 0;
  640. }
  641. /* called when interface is brought down by ifconfig */
  642. static int hso_net_close(struct net_device *net)
  643. {
  644. struct hso_net *odev = netdev_priv(net);
  645. /* we don't need the queue anymore */
  646. netif_stop_queue(net);
  647. /* no longer running */
  648. clear_bit(HSO_NET_RUNNING, &odev->flags);
  649. hso_stop_net_device(odev->parent);
  650. /* done */
  651. return 0;
  652. }
  653. /* USB tells is xmit done, we should start the netqueue again */
  654. static void write_bulk_callback(struct urb *urb)
  655. {
  656. struct hso_net *odev = urb->context;
  657. int status = urb->status;
  658. /* Sanity check */
  659. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  660. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  661. return;
  662. }
  663. /* Do we still have a valid kernel network device? */
  664. if (!netif_device_present(odev->net)) {
  665. dev_err(&urb->dev->dev, "%s: net device not present\n",
  666. __func__);
  667. return;
  668. }
  669. /* log status, but don't act on it, we don't need to resubmit anything
  670. * anyhow */
  671. if (status)
  672. handle_usb_error(status, __func__, odev->parent);
  673. hso_put_activity(odev->parent);
  674. /* Tell the network interface we are ready for another frame */
  675. netif_wake_queue(odev->net);
  676. }
  677. /* called by kernel when we need to transmit a packet */
  678. static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
  679. struct net_device *net)
  680. {
  681. struct hso_net *odev = netdev_priv(net);
  682. int result;
  683. /* Tell the kernel, "No more frames 'til we are done with this one." */
  684. netif_stop_queue(net);
  685. if (hso_get_activity(odev->parent) == -EAGAIN) {
  686. odev->skb_tx_buf = skb;
  687. return NETDEV_TX_OK;
  688. }
  689. /* log if asked */
  690. DUMP1(skb->data, skb->len);
  691. /* Copy it from kernel memory to OUR memory */
  692. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  693. hso_dbg(0x1, "len: %d/%d\n", skb->len, MUX_BULK_TX_BUF_SIZE);
  694. /* Fill in the URB for shipping it out. */
  695. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  696. odev->parent->usb,
  697. usb_sndbulkpipe(odev->parent->usb,
  698. odev->out_endp->
  699. bEndpointAddress & 0x7F),
  700. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  701. odev);
  702. /* Deal with the Zero Length packet problem, I hope */
  703. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  704. /* Send the URB on its merry way. */
  705. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  706. if (result) {
  707. dev_warn(&odev->parent->interface->dev,
  708. "failed mux_bulk_tx_urb %d\n", result);
  709. net->stats.tx_errors++;
  710. netif_start_queue(net);
  711. } else {
  712. net->stats.tx_packets++;
  713. net->stats.tx_bytes += skb->len;
  714. }
  715. dev_kfree_skb(skb);
  716. /* we're done */
  717. return NETDEV_TX_OK;
  718. }
  719. static const struct ethtool_ops ops = {
  720. .get_link = ethtool_op_get_link
  721. };
  722. /* called when a packet did not ack after watchdogtimeout */
  723. static void hso_net_tx_timeout(struct net_device *net)
  724. {
  725. struct hso_net *odev = netdev_priv(net);
  726. if (!odev)
  727. return;
  728. /* Tell syslog we are hosed. */
  729. dev_warn(&net->dev, "Tx timed out.\n");
  730. /* Tear the waiting frame off the list */
  731. if (odev->mux_bulk_tx_urb &&
  732. (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  733. usb_unlink_urb(odev->mux_bulk_tx_urb);
  734. /* Update statistics */
  735. net->stats.tx_errors++;
  736. }
  737. /* make a real packet from the received USB buffer */
  738. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  739. unsigned int count, unsigned char is_eop)
  740. {
  741. unsigned short temp_bytes;
  742. unsigned short buffer_offset = 0;
  743. unsigned short frame_len;
  744. unsigned char *tmp_rx_buf;
  745. /* log if needed */
  746. hso_dbg(0x1, "Rx %d bytes\n", count);
  747. DUMP(ip_pkt, min(128, (int)count));
  748. while (count) {
  749. switch (odev->rx_parse_state) {
  750. case WAIT_IP:
  751. /* waiting for IP header. */
  752. /* wanted bytes - size of ip header */
  753. temp_bytes =
  754. (count <
  755. odev->rx_buf_missing) ? count : odev->
  756. rx_buf_missing;
  757. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  758. odev->rx_buf_size, ip_pkt + buffer_offset,
  759. temp_bytes);
  760. odev->rx_buf_size += temp_bytes;
  761. buffer_offset += temp_bytes;
  762. odev->rx_buf_missing -= temp_bytes;
  763. count -= temp_bytes;
  764. if (!odev->rx_buf_missing) {
  765. /* header is complete allocate an sk_buffer and
  766. * continue to WAIT_DATA */
  767. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  768. if ((frame_len > DEFAULT_MRU) ||
  769. (frame_len < sizeof(struct iphdr))) {
  770. dev_err(&odev->net->dev,
  771. "Invalid frame (%d) length\n",
  772. frame_len);
  773. odev->rx_parse_state = WAIT_SYNC;
  774. continue;
  775. }
  776. /* Allocate an sk_buff */
  777. odev->skb_rx_buf = netdev_alloc_skb(odev->net,
  778. frame_len);
  779. if (!odev->skb_rx_buf) {
  780. /* We got no receive buffer. */
  781. hso_dbg(0x1, "could not allocate memory\n");
  782. odev->rx_parse_state = WAIT_SYNC;
  783. continue;
  784. }
  785. /* Copy what we got so far. make room for iphdr
  786. * after tail. */
  787. tmp_rx_buf =
  788. skb_put(odev->skb_rx_buf,
  789. sizeof(struct iphdr));
  790. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  791. sizeof(struct iphdr));
  792. /* ETH_HLEN */
  793. odev->rx_buf_size = sizeof(struct iphdr);
  794. /* Filip actually use .tot_len */
  795. odev->rx_buf_missing =
  796. frame_len - sizeof(struct iphdr);
  797. odev->rx_parse_state = WAIT_DATA;
  798. }
  799. break;
  800. case WAIT_DATA:
  801. temp_bytes = (count < odev->rx_buf_missing)
  802. ? count : odev->rx_buf_missing;
  803. /* Copy the rest of the bytes that are left in the
  804. * buffer into the waiting sk_buf. */
  805. /* Make room for temp_bytes after tail. */
  806. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  807. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  808. odev->rx_buf_missing -= temp_bytes;
  809. count -= temp_bytes;
  810. buffer_offset += temp_bytes;
  811. odev->rx_buf_size += temp_bytes;
  812. if (!odev->rx_buf_missing) {
  813. /* Packet is complete. Inject into stack. */
  814. /* We have IP packet here */
  815. odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
  816. skb_reset_mac_header(odev->skb_rx_buf);
  817. /* Ship it off to the kernel */
  818. netif_rx(odev->skb_rx_buf);
  819. /* No longer our buffer. */
  820. odev->skb_rx_buf = NULL;
  821. /* update out statistics */
  822. odev->net->stats.rx_packets++;
  823. odev->net->stats.rx_bytes += odev->rx_buf_size;
  824. odev->rx_buf_size = 0;
  825. odev->rx_buf_missing = sizeof(struct iphdr);
  826. odev->rx_parse_state = WAIT_IP;
  827. }
  828. break;
  829. case WAIT_SYNC:
  830. hso_dbg(0x1, " W_S\n");
  831. count = 0;
  832. break;
  833. default:
  834. hso_dbg(0x1, "\n");
  835. count--;
  836. break;
  837. }
  838. }
  839. /* Recovery mechanism for WAIT_SYNC state. */
  840. if (is_eop) {
  841. if (odev->rx_parse_state == WAIT_SYNC) {
  842. odev->rx_parse_state = WAIT_IP;
  843. odev->rx_buf_size = 0;
  844. odev->rx_buf_missing = sizeof(struct iphdr);
  845. }
  846. }
  847. }
  848. static void fix_crc_bug(struct urb *urb, __le16 max_packet_size)
  849. {
  850. static const u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  851. u32 rest = urb->actual_length % le16_to_cpu(max_packet_size);
  852. if (((rest == 5) || (rest == 6)) &&
  853. !memcmp(((u8 *)urb->transfer_buffer) + urb->actual_length - 4,
  854. crc_check, 4)) {
  855. urb->actual_length -= 4;
  856. }
  857. }
  858. /* Moving data from usb to kernel (in interrupt state) */
  859. static void read_bulk_callback(struct urb *urb)
  860. {
  861. struct hso_net *odev = urb->context;
  862. struct net_device *net;
  863. int result;
  864. int status = urb->status;
  865. /* is al ok? (Filip: Who's Al ?) */
  866. if (status) {
  867. handle_usb_error(status, __func__, odev->parent);
  868. return;
  869. }
  870. /* Sanity check */
  871. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  872. hso_dbg(0x1, "BULK IN callback but driver is not active!\n");
  873. return;
  874. }
  875. usb_mark_last_busy(urb->dev);
  876. net = odev->net;
  877. if (!netif_device_present(net)) {
  878. /* Somebody killed our network interface... */
  879. return;
  880. }
  881. if (odev->parent->port_spec & HSO_INFO_CRC_BUG)
  882. fix_crc_bug(urb, odev->in_endp->wMaxPacketSize);
  883. /* do we even have a packet? */
  884. if (urb->actual_length) {
  885. /* Handle the IP stream, add header and push it onto network
  886. * stack if the packet is complete. */
  887. spin_lock(&odev->net_lock);
  888. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  889. (urb->transfer_buffer_length >
  890. urb->actual_length) ? 1 : 0);
  891. spin_unlock(&odev->net_lock);
  892. }
  893. /* We are done with this URB, resubmit it. Prep the USB to wait for
  894. * another frame. Reuse same as received. */
  895. usb_fill_bulk_urb(urb,
  896. odev->parent->usb,
  897. usb_rcvbulkpipe(odev->parent->usb,
  898. odev->in_endp->
  899. bEndpointAddress & 0x7F),
  900. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  901. read_bulk_callback, odev);
  902. /* Give this to the USB subsystem so it can tell us when more data
  903. * arrives. */
  904. result = usb_submit_urb(urb, GFP_ATOMIC);
  905. if (result)
  906. dev_warn(&odev->parent->interface->dev,
  907. "%s failed submit mux_bulk_rx_urb %d\n", __func__,
  908. result);
  909. }
  910. /* Serial driver functions */
  911. static void hso_init_termios(struct ktermios *termios)
  912. {
  913. /*
  914. * The default requirements for this device are:
  915. */
  916. termios->c_iflag &=
  917. ~(IGNBRK /* disable ignore break */
  918. | BRKINT /* disable break causes interrupt */
  919. | PARMRK /* disable mark parity errors */
  920. | ISTRIP /* disable clear high bit of input characters */
  921. | INLCR /* disable translate NL to CR */
  922. | IGNCR /* disable ignore CR */
  923. | ICRNL /* disable translate CR to NL */
  924. | IXON); /* disable enable XON/XOFF flow control */
  925. /* disable postprocess output characters */
  926. termios->c_oflag &= ~OPOST;
  927. termios->c_lflag &=
  928. ~(ECHO /* disable echo input characters */
  929. | ECHONL /* disable echo new line */
  930. | ICANON /* disable erase, kill, werase, and rprnt
  931. special characters */
  932. | ISIG /* disable interrupt, quit, and suspend special
  933. characters */
  934. | IEXTEN); /* disable non-POSIX special characters */
  935. termios->c_cflag &=
  936. ~(CSIZE /* no size */
  937. | PARENB /* disable parity bit */
  938. | CBAUD /* clear current baud rate */
  939. | CBAUDEX); /* clear current buad rate */
  940. termios->c_cflag |= CS8; /* character size 8 bits */
  941. /* baud rate 115200 */
  942. tty_termios_encode_baud_rate(termios, 115200, 115200);
  943. }
  944. static void _hso_serial_set_termios(struct tty_struct *tty,
  945. struct ktermios *old)
  946. {
  947. struct hso_serial *serial = tty->driver_data;
  948. if (!serial) {
  949. pr_err("%s: no tty structures", __func__);
  950. return;
  951. }
  952. hso_dbg(0x8, "port %d\n", serial->minor);
  953. /*
  954. * Fix up unsupported bits
  955. */
  956. tty->termios.c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  957. tty->termios.c_cflag &=
  958. ~(CSIZE /* no size */
  959. | PARENB /* disable parity bit */
  960. | CBAUD /* clear current baud rate */
  961. | CBAUDEX); /* clear current buad rate */
  962. tty->termios.c_cflag |= CS8; /* character size 8 bits */
  963. /* baud rate 115200 */
  964. tty_encode_baud_rate(tty, 115200, 115200);
  965. }
  966. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  967. {
  968. int result;
  969. /* We are done with this URB, resubmit it. Prep the USB to wait for
  970. * another frame */
  971. usb_fill_bulk_urb(urb, serial->parent->usb,
  972. usb_rcvbulkpipe(serial->parent->usb,
  973. serial->in_endp->
  974. bEndpointAddress & 0x7F),
  975. urb->transfer_buffer, serial->rx_data_length,
  976. hso_std_serial_read_bulk_callback, serial);
  977. /* Give this to the USB subsystem so it can tell us when more data
  978. * arrives. */
  979. result = usb_submit_urb(urb, GFP_ATOMIC);
  980. if (result) {
  981. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  982. __func__, result);
  983. }
  984. }
  985. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  986. {
  987. int count;
  988. struct urb *curr_urb;
  989. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  990. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  991. count = put_rxbuf_data(curr_urb, serial);
  992. if (count == -1)
  993. return;
  994. if (count == 0) {
  995. serial->curr_rx_urb_idx++;
  996. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  997. serial->curr_rx_urb_idx = 0;
  998. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  999. }
  1000. }
  1001. }
  1002. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  1003. {
  1004. int count = 0;
  1005. struct urb *urb;
  1006. urb = serial->rx_urb[0];
  1007. if (serial->port.count > 0) {
  1008. count = put_rxbuf_data(urb, serial);
  1009. if (count == -1)
  1010. return;
  1011. }
  1012. /* Re issue a read as long as we receive data. */
  1013. if (count == 0 && ((urb->actual_length != 0) ||
  1014. (serial->rx_state == RX_PENDING))) {
  1015. serial->rx_state = RX_SENT;
  1016. hso_mux_serial_read(serial);
  1017. } else
  1018. serial->rx_state = RX_IDLE;
  1019. }
  1020. /* read callback for Diag and CS port */
  1021. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1022. {
  1023. struct hso_serial *serial = urb->context;
  1024. int status = urb->status;
  1025. hso_dbg(0x8, "--- Got serial_read_bulk callback %02x ---\n", status);
  1026. /* sanity check */
  1027. if (!serial) {
  1028. hso_dbg(0x1, "serial == NULL\n");
  1029. return;
  1030. }
  1031. if (status) {
  1032. handle_usb_error(status, __func__, serial->parent);
  1033. return;
  1034. }
  1035. hso_dbg(0x1, "Actual length = %d\n", urb->actual_length);
  1036. DUMP1(urb->transfer_buffer, urb->actual_length);
  1037. /* Anyone listening? */
  1038. if (serial->port.count == 0)
  1039. return;
  1040. if (serial->parent->port_spec & HSO_INFO_CRC_BUG)
  1041. fix_crc_bug(urb, serial->in_endp->wMaxPacketSize);
  1042. /* Valid data, handle RX data */
  1043. spin_lock(&serial->serial_lock);
  1044. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1045. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1046. spin_unlock(&serial->serial_lock);
  1047. }
  1048. /*
  1049. * This needs to be a tasklet otherwise we will
  1050. * end up recursively calling this function.
  1051. */
  1052. static void hso_unthrottle_tasklet(struct hso_serial *serial)
  1053. {
  1054. unsigned long flags;
  1055. spin_lock_irqsave(&serial->serial_lock, flags);
  1056. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1057. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1058. else
  1059. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1060. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1061. }
  1062. static void hso_unthrottle(struct tty_struct *tty)
  1063. {
  1064. struct hso_serial *serial = tty->driver_data;
  1065. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1066. }
  1067. /* open the requested serial port */
  1068. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1069. {
  1070. struct hso_serial *serial = get_serial_by_index(tty->index);
  1071. int result;
  1072. /* sanity check */
  1073. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1074. WARN_ON(1);
  1075. tty->driver_data = NULL;
  1076. hso_dbg(0x1, "Failed to open port\n");
  1077. return -ENODEV;
  1078. }
  1079. mutex_lock(&serial->parent->mutex);
  1080. result = usb_autopm_get_interface(serial->parent->interface);
  1081. if (result < 0)
  1082. goto err_out;
  1083. hso_dbg(0x1, "Opening %d\n", serial->minor);
  1084. /* setup */
  1085. tty->driver_data = serial;
  1086. tty_port_tty_set(&serial->port, tty);
  1087. /* check for port already opened, if not set the termios */
  1088. serial->port.count++;
  1089. if (serial->port.count == 1) {
  1090. serial->rx_state = RX_IDLE;
  1091. /* Force default termio settings */
  1092. _hso_serial_set_termios(tty, NULL);
  1093. tasklet_init(&serial->unthrottle_tasklet,
  1094. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1095. (unsigned long)serial);
  1096. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1097. if (result) {
  1098. hso_stop_serial_device(serial->parent);
  1099. serial->port.count--;
  1100. } else {
  1101. kref_get(&serial->parent->ref);
  1102. }
  1103. } else {
  1104. hso_dbg(0x1, "Port was already open\n");
  1105. }
  1106. usb_autopm_put_interface(serial->parent->interface);
  1107. /* done */
  1108. if (result)
  1109. hso_serial_tiocmset(tty, TIOCM_RTS | TIOCM_DTR, 0);
  1110. err_out:
  1111. mutex_unlock(&serial->parent->mutex);
  1112. return result;
  1113. }
  1114. /* close the requested serial port */
  1115. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1116. {
  1117. struct hso_serial *serial = tty->driver_data;
  1118. u8 usb_gone;
  1119. hso_dbg(0x1, "Closing serial port\n");
  1120. /* Open failed, no close cleanup required */
  1121. if (serial == NULL)
  1122. return;
  1123. mutex_lock(&serial->parent->mutex);
  1124. usb_gone = serial->parent->usb_gone;
  1125. if (!usb_gone)
  1126. usb_autopm_get_interface(serial->parent->interface);
  1127. /* reset the rts and dtr */
  1128. /* do the actual close */
  1129. serial->port.count--;
  1130. if (serial->port.count <= 0) {
  1131. serial->port.count = 0;
  1132. tty_port_tty_set(&serial->port, NULL);
  1133. if (!usb_gone)
  1134. hso_stop_serial_device(serial->parent);
  1135. tasklet_kill(&serial->unthrottle_tasklet);
  1136. }
  1137. if (!usb_gone)
  1138. usb_autopm_put_interface(serial->parent->interface);
  1139. mutex_unlock(&serial->parent->mutex);
  1140. }
  1141. /* close the requested serial port */
  1142. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1143. int count)
  1144. {
  1145. struct hso_serial *serial = tty->driver_data;
  1146. int space, tx_bytes;
  1147. unsigned long flags;
  1148. /* sanity check */
  1149. if (serial == NULL) {
  1150. pr_err("%s: serial is NULL\n", __func__);
  1151. return -ENODEV;
  1152. }
  1153. spin_lock_irqsave(&serial->serial_lock, flags);
  1154. space = serial->tx_data_length - serial->tx_buffer_count;
  1155. tx_bytes = (count < space) ? count : space;
  1156. if (!tx_bytes)
  1157. goto out;
  1158. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1159. serial->tx_buffer_count += tx_bytes;
  1160. out:
  1161. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1162. hso_kick_transmit(serial);
  1163. /* done */
  1164. return tx_bytes;
  1165. }
  1166. /* how much room is there for writing */
  1167. static int hso_serial_write_room(struct tty_struct *tty)
  1168. {
  1169. struct hso_serial *serial = tty->driver_data;
  1170. int room;
  1171. unsigned long flags;
  1172. spin_lock_irqsave(&serial->serial_lock, flags);
  1173. room = serial->tx_data_length - serial->tx_buffer_count;
  1174. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1175. /* return free room */
  1176. return room;
  1177. }
  1178. static void hso_serial_cleanup(struct tty_struct *tty)
  1179. {
  1180. struct hso_serial *serial = tty->driver_data;
  1181. if (!serial)
  1182. return;
  1183. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1184. }
  1185. /* setup the term */
  1186. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1187. {
  1188. struct hso_serial *serial = tty->driver_data;
  1189. unsigned long flags;
  1190. if (old)
  1191. hso_dbg(0x16, "Termios called with: cflags new[%d] - old[%d]\n",
  1192. tty->termios.c_cflag, old->c_cflag);
  1193. /* the actual setup */
  1194. spin_lock_irqsave(&serial->serial_lock, flags);
  1195. if (serial->port.count)
  1196. _hso_serial_set_termios(tty, old);
  1197. else
  1198. tty->termios = *old;
  1199. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1200. /* done */
  1201. }
  1202. /* how many characters in the buffer */
  1203. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1204. {
  1205. struct hso_serial *serial = tty->driver_data;
  1206. int chars;
  1207. unsigned long flags;
  1208. /* sanity check */
  1209. if (serial == NULL)
  1210. return 0;
  1211. spin_lock_irqsave(&serial->serial_lock, flags);
  1212. chars = serial->tx_buffer_count;
  1213. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1214. return chars;
  1215. }
  1216. static int tiocmget_submit_urb(struct hso_serial *serial,
  1217. struct hso_tiocmget *tiocmget,
  1218. struct usb_device *usb)
  1219. {
  1220. int result;
  1221. if (serial->parent->usb_gone)
  1222. return -ENODEV;
  1223. usb_fill_int_urb(tiocmget->urb, usb,
  1224. usb_rcvintpipe(usb,
  1225. tiocmget->endp->
  1226. bEndpointAddress & 0x7F),
  1227. &tiocmget->serial_state_notification,
  1228. sizeof(struct hso_serial_state_notification),
  1229. tiocmget_intr_callback, serial,
  1230. tiocmget->endp->bInterval);
  1231. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1232. if (result) {
  1233. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1234. result);
  1235. }
  1236. return result;
  1237. }
  1238. static void tiocmget_intr_callback(struct urb *urb)
  1239. {
  1240. struct hso_serial *serial = urb->context;
  1241. struct hso_tiocmget *tiocmget;
  1242. int status = urb->status;
  1243. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1244. struct uart_icount *icount;
  1245. struct hso_serial_state_notification *serial_state_notification;
  1246. struct usb_device *usb;
  1247. struct usb_interface *interface;
  1248. int if_num;
  1249. /* Sanity checks */
  1250. if (!serial)
  1251. return;
  1252. if (status) {
  1253. handle_usb_error(status, __func__, serial->parent);
  1254. return;
  1255. }
  1256. /* tiocmget is only supported on HSO_PORT_MODEM */
  1257. tiocmget = serial->tiocmget;
  1258. if (!tiocmget)
  1259. return;
  1260. BUG_ON((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM);
  1261. usb = serial->parent->usb;
  1262. interface = serial->parent->interface;
  1263. if_num = interface->cur_altsetting->desc.bInterfaceNumber;
  1264. /* wIndex should be the USB interface number of the port to which the
  1265. * notification applies, which should always be the Modem port.
  1266. */
  1267. serial_state_notification = &tiocmget->serial_state_notification;
  1268. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1269. serial_state_notification->bNotification != B_NOTIFICATION ||
  1270. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1271. le16_to_cpu(serial_state_notification->wIndex) != if_num ||
  1272. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1273. dev_warn(&usb->dev,
  1274. "hso received invalid serial state notification\n");
  1275. DUMP(serial_state_notification,
  1276. sizeof(struct hso_serial_state_notification));
  1277. } else {
  1278. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1279. UART_state_bitmap);
  1280. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1281. icount = &tiocmget->icount;
  1282. spin_lock(&serial->serial_lock);
  1283. if ((UART_state_bitmap & B_OVERRUN) !=
  1284. (prev_UART_state_bitmap & B_OVERRUN))
  1285. icount->parity++;
  1286. if ((UART_state_bitmap & B_PARITY) !=
  1287. (prev_UART_state_bitmap & B_PARITY))
  1288. icount->parity++;
  1289. if ((UART_state_bitmap & B_FRAMING) !=
  1290. (prev_UART_state_bitmap & B_FRAMING))
  1291. icount->frame++;
  1292. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1293. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1294. icount->rng++;
  1295. if ((UART_state_bitmap & B_BREAK) !=
  1296. (prev_UART_state_bitmap & B_BREAK))
  1297. icount->brk++;
  1298. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1299. (prev_UART_state_bitmap & B_TX_CARRIER))
  1300. icount->dsr++;
  1301. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1302. (prev_UART_state_bitmap & B_RX_CARRIER))
  1303. icount->dcd++;
  1304. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1305. spin_unlock(&serial->serial_lock);
  1306. tiocmget->intr_completed = 1;
  1307. wake_up_interruptible(&tiocmget->waitq);
  1308. }
  1309. memset(serial_state_notification, 0,
  1310. sizeof(struct hso_serial_state_notification));
  1311. tiocmget_submit_urb(serial,
  1312. tiocmget,
  1313. serial->parent->usb);
  1314. }
  1315. /*
  1316. * next few functions largely stolen from drivers/serial/serial_core.c
  1317. */
  1318. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1319. * - mask passed in arg for lines of interest
  1320. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1321. * Caller should use TIOCGICOUNT to see which one it was
  1322. */
  1323. static int
  1324. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1325. {
  1326. DECLARE_WAITQUEUE(wait, current);
  1327. struct uart_icount cprev, cnow;
  1328. struct hso_tiocmget *tiocmget;
  1329. int ret;
  1330. tiocmget = serial->tiocmget;
  1331. if (!tiocmget)
  1332. return -ENOENT;
  1333. /*
  1334. * note the counters on entry
  1335. */
  1336. spin_lock_irq(&serial->serial_lock);
  1337. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1338. spin_unlock_irq(&serial->serial_lock);
  1339. add_wait_queue(&tiocmget->waitq, &wait);
  1340. for (;;) {
  1341. spin_lock_irq(&serial->serial_lock);
  1342. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1343. spin_unlock_irq(&serial->serial_lock);
  1344. set_current_state(TASK_INTERRUPTIBLE);
  1345. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1346. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1347. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1348. ret = 0;
  1349. break;
  1350. }
  1351. schedule();
  1352. /* see if a signal did it */
  1353. if (signal_pending(current)) {
  1354. ret = -ERESTARTSYS;
  1355. break;
  1356. }
  1357. cprev = cnow;
  1358. }
  1359. __set_current_state(TASK_RUNNING);
  1360. remove_wait_queue(&tiocmget->waitq, &wait);
  1361. return ret;
  1362. }
  1363. /*
  1364. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1365. * Return: write counters to the user passed counter struct
  1366. * NB: both 1->0 and 0->1 transitions are counted except for
  1367. * RI where only 0->1 is counted.
  1368. */
  1369. static int hso_get_count(struct tty_struct *tty,
  1370. struct serial_icounter_struct *icount)
  1371. {
  1372. struct uart_icount cnow;
  1373. struct hso_serial *serial = tty->driver_data;
  1374. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1375. memset(icount, 0, sizeof(struct serial_icounter_struct));
  1376. if (!tiocmget)
  1377. return -ENOENT;
  1378. spin_lock_irq(&serial->serial_lock);
  1379. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1380. spin_unlock_irq(&serial->serial_lock);
  1381. icount->cts = cnow.cts;
  1382. icount->dsr = cnow.dsr;
  1383. icount->rng = cnow.rng;
  1384. icount->dcd = cnow.dcd;
  1385. icount->rx = cnow.rx;
  1386. icount->tx = cnow.tx;
  1387. icount->frame = cnow.frame;
  1388. icount->overrun = cnow.overrun;
  1389. icount->parity = cnow.parity;
  1390. icount->brk = cnow.brk;
  1391. icount->buf_overrun = cnow.buf_overrun;
  1392. return 0;
  1393. }
  1394. static int hso_serial_tiocmget(struct tty_struct *tty)
  1395. {
  1396. int retval;
  1397. struct hso_serial *serial = tty->driver_data;
  1398. struct hso_tiocmget *tiocmget;
  1399. u16 UART_state_bitmap;
  1400. /* sanity check */
  1401. if (!serial) {
  1402. hso_dbg(0x1, "no tty structures\n");
  1403. return -EINVAL;
  1404. }
  1405. spin_lock_irq(&serial->serial_lock);
  1406. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1407. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1408. tiocmget = serial->tiocmget;
  1409. if (tiocmget) {
  1410. UART_state_bitmap = le16_to_cpu(
  1411. tiocmget->prev_UART_state_bitmap);
  1412. if (UART_state_bitmap & B_RING_SIGNAL)
  1413. retval |= TIOCM_RNG;
  1414. if (UART_state_bitmap & B_RX_CARRIER)
  1415. retval |= TIOCM_CD;
  1416. if (UART_state_bitmap & B_TX_CARRIER)
  1417. retval |= TIOCM_DSR;
  1418. }
  1419. spin_unlock_irq(&serial->serial_lock);
  1420. return retval;
  1421. }
  1422. static int hso_serial_tiocmset(struct tty_struct *tty,
  1423. unsigned int set, unsigned int clear)
  1424. {
  1425. int val = 0;
  1426. unsigned long flags;
  1427. int if_num;
  1428. struct hso_serial *serial = tty->driver_data;
  1429. struct usb_interface *interface;
  1430. /* sanity check */
  1431. if (!serial) {
  1432. hso_dbg(0x1, "no tty structures\n");
  1433. return -EINVAL;
  1434. }
  1435. if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
  1436. return -EINVAL;
  1437. interface = serial->parent->interface;
  1438. if_num = interface->cur_altsetting->desc.bInterfaceNumber;
  1439. spin_lock_irqsave(&serial->serial_lock, flags);
  1440. if (set & TIOCM_RTS)
  1441. serial->rts_state = 1;
  1442. if (set & TIOCM_DTR)
  1443. serial->dtr_state = 1;
  1444. if (clear & TIOCM_RTS)
  1445. serial->rts_state = 0;
  1446. if (clear & TIOCM_DTR)
  1447. serial->dtr_state = 0;
  1448. if (serial->dtr_state)
  1449. val |= 0x01;
  1450. if (serial->rts_state)
  1451. val |= 0x02;
  1452. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1453. return usb_control_msg(serial->parent->usb,
  1454. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1455. 0x21, val, if_num, NULL, 0,
  1456. USB_CTRL_SET_TIMEOUT);
  1457. }
  1458. static int hso_serial_ioctl(struct tty_struct *tty,
  1459. unsigned int cmd, unsigned long arg)
  1460. {
  1461. struct hso_serial *serial = tty->driver_data;
  1462. int ret = 0;
  1463. hso_dbg(0x8, "IOCTL cmd: %d, arg: %ld\n", cmd, arg);
  1464. if (!serial)
  1465. return -ENODEV;
  1466. switch (cmd) {
  1467. case TIOCMIWAIT:
  1468. ret = hso_wait_modem_status(serial, arg);
  1469. break;
  1470. default:
  1471. ret = -ENOIOCTLCMD;
  1472. break;
  1473. }
  1474. return ret;
  1475. }
  1476. /* starts a transmit */
  1477. static void hso_kick_transmit(struct hso_serial *serial)
  1478. {
  1479. u8 *temp;
  1480. unsigned long flags;
  1481. int res;
  1482. spin_lock_irqsave(&serial->serial_lock, flags);
  1483. if (!serial->tx_buffer_count)
  1484. goto out;
  1485. if (serial->tx_urb_used)
  1486. goto out;
  1487. /* Wakeup USB interface if necessary */
  1488. if (hso_get_activity(serial->parent) == -EAGAIN)
  1489. goto out;
  1490. /* Switch pointers around to avoid memcpy */
  1491. temp = serial->tx_buffer;
  1492. serial->tx_buffer = serial->tx_data;
  1493. serial->tx_data = temp;
  1494. serial->tx_data_count = serial->tx_buffer_count;
  1495. serial->tx_buffer_count = 0;
  1496. /* If temp is set, it means we switched buffers */
  1497. if (temp && serial->write_data) {
  1498. res = serial->write_data(serial);
  1499. if (res >= 0)
  1500. serial->tx_urb_used = 1;
  1501. }
  1502. out:
  1503. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1504. }
  1505. /* make a request (for reading and writing data to muxed serial port) */
  1506. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1507. struct urb *ctrl_urb,
  1508. struct usb_ctrlrequest *ctrl_req,
  1509. u8 *ctrl_urb_data, u32 size)
  1510. {
  1511. int result;
  1512. int pipe;
  1513. /* Sanity check */
  1514. if (!serial || !ctrl_urb || !ctrl_req) {
  1515. pr_err("%s: Wrong arguments\n", __func__);
  1516. return -EINVAL;
  1517. }
  1518. /* initialize */
  1519. ctrl_req->wValue = 0;
  1520. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1521. ctrl_req->wLength = cpu_to_le16(size);
  1522. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1523. /* Reading command */
  1524. ctrl_req->bRequestType = USB_DIR_IN |
  1525. USB_TYPE_OPTION_VENDOR |
  1526. USB_RECIP_INTERFACE;
  1527. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1528. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1529. } else {
  1530. /* Writing command */
  1531. ctrl_req->bRequestType = USB_DIR_OUT |
  1532. USB_TYPE_OPTION_VENDOR |
  1533. USB_RECIP_INTERFACE;
  1534. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1535. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1536. }
  1537. /* syslog */
  1538. hso_dbg(0x2, "%s command (%02x) len: %d, port: %d\n",
  1539. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1540. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1541. /* Load ctrl urb */
  1542. ctrl_urb->transfer_flags = 0;
  1543. usb_fill_control_urb(ctrl_urb,
  1544. serial->parent->usb,
  1545. pipe,
  1546. (u8 *) ctrl_req,
  1547. ctrl_urb_data, size, ctrl_callback, serial);
  1548. /* Send it on merry way */
  1549. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1550. if (result) {
  1551. dev_err(&ctrl_urb->dev->dev,
  1552. "%s failed submit ctrl_urb %d type %d\n", __func__,
  1553. result, type);
  1554. return result;
  1555. }
  1556. /* done */
  1557. return size;
  1558. }
  1559. /* called by intr_callback when read occurs */
  1560. static int hso_mux_serial_read(struct hso_serial *serial)
  1561. {
  1562. if (!serial)
  1563. return -EINVAL;
  1564. /* clean data */
  1565. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1566. /* make the request */
  1567. if (serial->num_rx_urbs != 1) {
  1568. dev_err(&serial->parent->interface->dev,
  1569. "ERROR: mux'd reads with multiple buffers "
  1570. "not possible\n");
  1571. return 0;
  1572. }
  1573. return mux_device_request(serial,
  1574. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1575. serial->parent->port_spec & HSO_PORT_MASK,
  1576. serial->rx_urb[0],
  1577. &serial->ctrl_req_rx,
  1578. serial->rx_data[0], serial->rx_data_length);
  1579. }
  1580. /* used for muxed serial port callback (muxed serial read) */
  1581. static void intr_callback(struct urb *urb)
  1582. {
  1583. struct hso_shared_int *shared_int = urb->context;
  1584. struct hso_serial *serial;
  1585. unsigned char *port_req;
  1586. int status = urb->status;
  1587. int i;
  1588. usb_mark_last_busy(urb->dev);
  1589. /* sanity check */
  1590. if (!shared_int)
  1591. return;
  1592. /* status check */
  1593. if (status) {
  1594. handle_usb_error(status, __func__, NULL);
  1595. return;
  1596. }
  1597. hso_dbg(0x8, "--- Got intr callback 0x%02X ---\n", status);
  1598. /* what request? */
  1599. port_req = urb->transfer_buffer;
  1600. hso_dbg(0x8, "port_req = 0x%.2X\n", *port_req);
  1601. /* loop over all muxed ports to find the one sending this */
  1602. for (i = 0; i < 8; i++) {
  1603. /* max 8 channels on MUX */
  1604. if (*port_req & (1 << i)) {
  1605. serial = get_serial_by_shared_int_and_type(shared_int,
  1606. (1 << i));
  1607. if (serial != NULL) {
  1608. hso_dbg(0x1, "Pending read interrupt on port %d\n",
  1609. i);
  1610. spin_lock(&serial->serial_lock);
  1611. if (serial->rx_state == RX_IDLE &&
  1612. serial->port.count > 0) {
  1613. /* Setup and send a ctrl req read on
  1614. * port i */
  1615. if (!serial->rx_urb_filled[0]) {
  1616. serial->rx_state = RX_SENT;
  1617. hso_mux_serial_read(serial);
  1618. } else
  1619. serial->rx_state = RX_PENDING;
  1620. } else {
  1621. hso_dbg(0x1, "Already a read pending on port %d or port not open\n",
  1622. i);
  1623. }
  1624. spin_unlock(&serial->serial_lock);
  1625. }
  1626. }
  1627. }
  1628. /* Resubmit interrupt urb */
  1629. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1630. }
  1631. /* called for writing to muxed serial port */
  1632. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1633. {
  1634. if (NULL == serial)
  1635. return -EINVAL;
  1636. return mux_device_request(serial,
  1637. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1638. serial->parent->port_spec & HSO_PORT_MASK,
  1639. serial->tx_urb,
  1640. &serial->ctrl_req_tx,
  1641. serial->tx_data, serial->tx_data_count);
  1642. }
  1643. /* write callback for Diag and CS port */
  1644. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1645. {
  1646. struct hso_serial *serial = urb->context;
  1647. int status = urb->status;
  1648. /* sanity check */
  1649. if (!serial) {
  1650. hso_dbg(0x1, "serial == NULL\n");
  1651. return;
  1652. }
  1653. spin_lock(&serial->serial_lock);
  1654. serial->tx_urb_used = 0;
  1655. spin_unlock(&serial->serial_lock);
  1656. if (status) {
  1657. handle_usb_error(status, __func__, serial->parent);
  1658. return;
  1659. }
  1660. hso_put_activity(serial->parent);
  1661. tty_port_tty_wakeup(&serial->port);
  1662. hso_kick_transmit(serial);
  1663. hso_dbg(0x1, "\n");
  1664. }
  1665. /* called for writing diag or CS serial port */
  1666. static int hso_std_serial_write_data(struct hso_serial *serial)
  1667. {
  1668. int count = serial->tx_data_count;
  1669. int result;
  1670. usb_fill_bulk_urb(serial->tx_urb,
  1671. serial->parent->usb,
  1672. usb_sndbulkpipe(serial->parent->usb,
  1673. serial->out_endp->
  1674. bEndpointAddress & 0x7F),
  1675. serial->tx_data, serial->tx_data_count,
  1676. hso_std_serial_write_bulk_callback, serial);
  1677. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1678. if (result) {
  1679. dev_warn(&serial->parent->usb->dev,
  1680. "Failed to submit urb - res %d\n", result);
  1681. return result;
  1682. }
  1683. return count;
  1684. }
  1685. /* callback after read or write on muxed serial port */
  1686. static void ctrl_callback(struct urb *urb)
  1687. {
  1688. struct hso_serial *serial = urb->context;
  1689. struct usb_ctrlrequest *req;
  1690. int status = urb->status;
  1691. /* sanity check */
  1692. if (!serial)
  1693. return;
  1694. spin_lock(&serial->serial_lock);
  1695. serial->tx_urb_used = 0;
  1696. spin_unlock(&serial->serial_lock);
  1697. if (status) {
  1698. handle_usb_error(status, __func__, serial->parent);
  1699. return;
  1700. }
  1701. /* what request? */
  1702. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1703. hso_dbg(0x8, "--- Got muxed ctrl callback 0x%02X ---\n", status);
  1704. hso_dbg(0x8, "Actual length of urb = %d\n", urb->actual_length);
  1705. DUMP1(urb->transfer_buffer, urb->actual_length);
  1706. if (req->bRequestType ==
  1707. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1708. /* response to a read command */
  1709. serial->rx_urb_filled[0] = 1;
  1710. spin_lock(&serial->serial_lock);
  1711. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1712. spin_unlock(&serial->serial_lock);
  1713. } else {
  1714. hso_put_activity(serial->parent);
  1715. tty_port_tty_wakeup(&serial->port);
  1716. /* response to a write command */
  1717. hso_kick_transmit(serial);
  1718. }
  1719. }
  1720. /* handle RX data for serial port */
  1721. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1722. {
  1723. struct tty_struct *tty;
  1724. int count;
  1725. /* Sanity check */
  1726. if (urb == NULL || serial == NULL) {
  1727. hso_dbg(0x1, "serial = NULL\n");
  1728. return -2;
  1729. }
  1730. tty = tty_port_tty_get(&serial->port);
  1731. if (tty && tty_throttled(tty)) {
  1732. tty_kref_put(tty);
  1733. return -1;
  1734. }
  1735. /* Push data to tty */
  1736. hso_dbg(0x1, "data to push to tty\n");
  1737. count = tty_buffer_request_room(&serial->port, urb->actual_length);
  1738. if (count >= urb->actual_length) {
  1739. tty_insert_flip_string(&serial->port, urb->transfer_buffer,
  1740. urb->actual_length);
  1741. tty_flip_buffer_push(&serial->port);
  1742. } else {
  1743. dev_warn(&serial->parent->usb->dev,
  1744. "dropping data, %d bytes lost\n", urb->actual_length);
  1745. }
  1746. tty_kref_put(tty);
  1747. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1748. return 0;
  1749. }
  1750. /* Base driver functions */
  1751. static void hso_log_port(struct hso_device *hso_dev)
  1752. {
  1753. char *port_type;
  1754. char port_dev[20];
  1755. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1756. case HSO_PORT_CONTROL:
  1757. port_type = "Control";
  1758. break;
  1759. case HSO_PORT_APP:
  1760. port_type = "Application";
  1761. break;
  1762. case HSO_PORT_GPS:
  1763. port_type = "GPS";
  1764. break;
  1765. case HSO_PORT_GPS_CONTROL:
  1766. port_type = "GPS control";
  1767. break;
  1768. case HSO_PORT_APP2:
  1769. port_type = "Application2";
  1770. break;
  1771. case HSO_PORT_PCSC:
  1772. port_type = "PCSC";
  1773. break;
  1774. case HSO_PORT_DIAG:
  1775. port_type = "Diagnostic";
  1776. break;
  1777. case HSO_PORT_DIAG2:
  1778. port_type = "Diagnostic2";
  1779. break;
  1780. case HSO_PORT_MODEM:
  1781. port_type = "Modem";
  1782. break;
  1783. case HSO_PORT_NETWORK:
  1784. port_type = "Network";
  1785. break;
  1786. default:
  1787. port_type = "Unknown";
  1788. break;
  1789. }
  1790. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1791. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1792. } else
  1793. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1794. dev2ser(hso_dev)->minor);
  1795. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1796. port_type, port_dev);
  1797. }
  1798. static int hso_start_net_device(struct hso_device *hso_dev)
  1799. {
  1800. int i, result = 0;
  1801. struct hso_net *hso_net = dev2net(hso_dev);
  1802. if (!hso_net)
  1803. return -ENODEV;
  1804. /* send URBs for all read buffers */
  1805. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1806. /* Prep a receive URB */
  1807. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1808. hso_dev->usb,
  1809. usb_rcvbulkpipe(hso_dev->usb,
  1810. hso_net->in_endp->
  1811. bEndpointAddress & 0x7F),
  1812. hso_net->mux_bulk_rx_buf_pool[i],
  1813. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1814. hso_net);
  1815. /* Put it out there so the device can send us stuff */
  1816. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1817. GFP_NOIO);
  1818. if (result)
  1819. dev_warn(&hso_dev->usb->dev,
  1820. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1821. i, result);
  1822. }
  1823. return result;
  1824. }
  1825. static int hso_stop_net_device(struct hso_device *hso_dev)
  1826. {
  1827. int i;
  1828. struct hso_net *hso_net = dev2net(hso_dev);
  1829. if (!hso_net)
  1830. return -ENODEV;
  1831. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1832. if (hso_net->mux_bulk_rx_urb_pool[i])
  1833. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1834. }
  1835. if (hso_net->mux_bulk_tx_urb)
  1836. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1837. return 0;
  1838. }
  1839. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1840. {
  1841. int i, result = 0;
  1842. struct hso_serial *serial = dev2ser(hso_dev);
  1843. if (!serial)
  1844. return -ENODEV;
  1845. /* If it is not the MUX port fill in and submit a bulk urb (already
  1846. * allocated in hso_serial_start) */
  1847. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1848. for (i = 0; i < serial->num_rx_urbs; i++) {
  1849. usb_fill_bulk_urb(serial->rx_urb[i],
  1850. serial->parent->usb,
  1851. usb_rcvbulkpipe(serial->parent->usb,
  1852. serial->in_endp->
  1853. bEndpointAddress &
  1854. 0x7F),
  1855. serial->rx_data[i],
  1856. serial->rx_data_length,
  1857. hso_std_serial_read_bulk_callback,
  1858. serial);
  1859. result = usb_submit_urb(serial->rx_urb[i], flags);
  1860. if (result) {
  1861. dev_warn(&serial->parent->usb->dev,
  1862. "Failed to submit urb - res %d\n",
  1863. result);
  1864. break;
  1865. }
  1866. }
  1867. } else {
  1868. mutex_lock(&serial->shared_int->shared_int_lock);
  1869. if (!serial->shared_int->use_count) {
  1870. result =
  1871. hso_mux_submit_intr_urb(serial->shared_int,
  1872. hso_dev->usb, flags);
  1873. }
  1874. serial->shared_int->use_count++;
  1875. mutex_unlock(&serial->shared_int->shared_int_lock);
  1876. }
  1877. if (serial->tiocmget)
  1878. tiocmget_submit_urb(serial,
  1879. serial->tiocmget,
  1880. serial->parent->usb);
  1881. return result;
  1882. }
  1883. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1884. {
  1885. int i;
  1886. struct hso_serial *serial = dev2ser(hso_dev);
  1887. struct hso_tiocmget *tiocmget;
  1888. if (!serial)
  1889. return -ENODEV;
  1890. for (i = 0; i < serial->num_rx_urbs; i++) {
  1891. if (serial->rx_urb[i]) {
  1892. usb_kill_urb(serial->rx_urb[i]);
  1893. serial->rx_urb_filled[i] = 0;
  1894. }
  1895. }
  1896. serial->curr_rx_urb_idx = 0;
  1897. if (serial->tx_urb)
  1898. usb_kill_urb(serial->tx_urb);
  1899. if (serial->shared_int) {
  1900. mutex_lock(&serial->shared_int->shared_int_lock);
  1901. if (serial->shared_int->use_count &&
  1902. (--serial->shared_int->use_count == 0)) {
  1903. struct urb *urb;
  1904. urb = serial->shared_int->shared_intr_urb;
  1905. if (urb)
  1906. usb_kill_urb(urb);
  1907. }
  1908. mutex_unlock(&serial->shared_int->shared_int_lock);
  1909. }
  1910. tiocmget = serial->tiocmget;
  1911. if (tiocmget) {
  1912. wake_up_interruptible(&tiocmget->waitq);
  1913. usb_kill_urb(tiocmget->urb);
  1914. }
  1915. return 0;
  1916. }
  1917. static void hso_serial_tty_unregister(struct hso_serial *serial)
  1918. {
  1919. tty_unregister_device(tty_drv, serial->minor);
  1920. }
  1921. static void hso_serial_common_free(struct hso_serial *serial)
  1922. {
  1923. int i;
  1924. for (i = 0; i < serial->num_rx_urbs; i++) {
  1925. /* unlink and free RX URB */
  1926. usb_free_urb(serial->rx_urb[i]);
  1927. /* free the RX buffer */
  1928. kfree(serial->rx_data[i]);
  1929. }
  1930. /* unlink and free TX URB */
  1931. usb_free_urb(serial->tx_urb);
  1932. kfree(serial->tx_buffer);
  1933. kfree(serial->tx_data);
  1934. tty_port_destroy(&serial->port);
  1935. }
  1936. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1937. int rx_size, int tx_size)
  1938. {
  1939. struct device *dev;
  1940. int minor;
  1941. int i;
  1942. tty_port_init(&serial->port);
  1943. minor = get_free_serial_index();
  1944. if (minor < 0)
  1945. goto exit;
  1946. /* register our minor number */
  1947. serial->parent->dev = tty_port_register_device_attr(&serial->port,
  1948. tty_drv, minor, &serial->parent->interface->dev,
  1949. serial->parent, hso_serial_dev_groups);
  1950. dev = serial->parent->dev;
  1951. /* fill in specific data for later use */
  1952. serial->minor = minor;
  1953. serial->magic = HSO_SERIAL_MAGIC;
  1954. spin_lock_init(&serial->serial_lock);
  1955. serial->num_rx_urbs = num_urbs;
  1956. /* RX, allocate urb and initialize */
  1957. /* prepare our RX buffer */
  1958. serial->rx_data_length = rx_size;
  1959. for (i = 0; i < serial->num_rx_urbs; i++) {
  1960. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  1961. if (!serial->rx_urb[i])
  1962. goto exit;
  1963. serial->rx_urb[i]->transfer_buffer = NULL;
  1964. serial->rx_urb[i]->transfer_buffer_length = 0;
  1965. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  1966. GFP_KERNEL);
  1967. if (!serial->rx_data[i])
  1968. goto exit;
  1969. }
  1970. /* TX, allocate urb and initialize */
  1971. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1972. if (!serial->tx_urb)
  1973. goto exit;
  1974. serial->tx_urb->transfer_buffer = NULL;
  1975. serial->tx_urb->transfer_buffer_length = 0;
  1976. /* prepare our TX buffer */
  1977. serial->tx_data_count = 0;
  1978. serial->tx_buffer_count = 0;
  1979. serial->tx_data_length = tx_size;
  1980. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1981. if (!serial->tx_data)
  1982. goto exit;
  1983. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1984. if (!serial->tx_buffer)
  1985. goto exit;
  1986. return 0;
  1987. exit:
  1988. hso_serial_tty_unregister(serial);
  1989. hso_serial_common_free(serial);
  1990. return -1;
  1991. }
  1992. /* Creates a general hso device */
  1993. static struct hso_device *hso_create_device(struct usb_interface *intf,
  1994. int port_spec)
  1995. {
  1996. struct hso_device *hso_dev;
  1997. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  1998. if (!hso_dev)
  1999. return NULL;
  2000. hso_dev->port_spec = port_spec;
  2001. hso_dev->usb = interface_to_usbdev(intf);
  2002. hso_dev->interface = intf;
  2003. kref_init(&hso_dev->ref);
  2004. mutex_init(&hso_dev->mutex);
  2005. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2006. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2007. return hso_dev;
  2008. }
  2009. /* Removes a network device in the network device table */
  2010. static int remove_net_device(struct hso_device *hso_dev)
  2011. {
  2012. int i;
  2013. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2014. if (network_table[i] == hso_dev) {
  2015. network_table[i] = NULL;
  2016. break;
  2017. }
  2018. }
  2019. if (i == HSO_MAX_NET_DEVICES)
  2020. return -1;
  2021. return 0;
  2022. }
  2023. /* Frees our network device */
  2024. static void hso_free_net_device(struct hso_device *hso_dev)
  2025. {
  2026. int i;
  2027. struct hso_net *hso_net = dev2net(hso_dev);
  2028. if (!hso_net)
  2029. return;
  2030. remove_net_device(hso_net->parent);
  2031. if (hso_net->net)
  2032. unregister_netdev(hso_net->net);
  2033. /* start freeing */
  2034. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2035. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2036. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2037. hso_net->mux_bulk_rx_buf_pool[i] = NULL;
  2038. }
  2039. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2040. kfree(hso_net->mux_bulk_tx_buf);
  2041. hso_net->mux_bulk_tx_buf = NULL;
  2042. if (hso_net->net)
  2043. free_netdev(hso_net->net);
  2044. kfree(hso_dev);
  2045. }
  2046. static const struct net_device_ops hso_netdev_ops = {
  2047. .ndo_open = hso_net_open,
  2048. .ndo_stop = hso_net_close,
  2049. .ndo_start_xmit = hso_net_start_xmit,
  2050. .ndo_tx_timeout = hso_net_tx_timeout,
  2051. };
  2052. /* initialize the network interface */
  2053. static void hso_net_init(struct net_device *net)
  2054. {
  2055. struct hso_net *hso_net = netdev_priv(net);
  2056. hso_dbg(0x1, "sizeof hso_net is %zu\n", sizeof(*hso_net));
  2057. /* fill in the other fields */
  2058. net->netdev_ops = &hso_netdev_ops;
  2059. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2060. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2061. net->type = ARPHRD_NONE;
  2062. net->mtu = DEFAULT_MTU - 14;
  2063. net->tx_queue_len = 10;
  2064. net->ethtool_ops = &ops;
  2065. /* and initialize the semaphore */
  2066. spin_lock_init(&hso_net->net_lock);
  2067. }
  2068. /* Adds a network device in the network device table */
  2069. static int add_net_device(struct hso_device *hso_dev)
  2070. {
  2071. int i;
  2072. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2073. if (network_table[i] == NULL) {
  2074. network_table[i] = hso_dev;
  2075. break;
  2076. }
  2077. }
  2078. if (i == HSO_MAX_NET_DEVICES)
  2079. return -1;
  2080. return 0;
  2081. }
  2082. static int hso_rfkill_set_block(void *data, bool blocked)
  2083. {
  2084. struct hso_device *hso_dev = data;
  2085. int enabled = !blocked;
  2086. int rv;
  2087. mutex_lock(&hso_dev->mutex);
  2088. if (hso_dev->usb_gone)
  2089. rv = 0;
  2090. else
  2091. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2092. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2093. USB_CTRL_SET_TIMEOUT);
  2094. mutex_unlock(&hso_dev->mutex);
  2095. return rv;
  2096. }
  2097. static const struct rfkill_ops hso_rfkill_ops = {
  2098. .set_block = hso_rfkill_set_block,
  2099. };
  2100. /* Creates and sets up everything for rfkill */
  2101. static void hso_create_rfkill(struct hso_device *hso_dev,
  2102. struct usb_interface *interface)
  2103. {
  2104. struct hso_net *hso_net = dev2net(hso_dev);
  2105. struct device *dev = &hso_net->net->dev;
  2106. static u32 rfkill_counter;
  2107. snprintf(hso_net->name, sizeof(hso_net->name), "hso-%d",
  2108. rfkill_counter++);
  2109. hso_net->rfkill = rfkill_alloc(hso_net->name,
  2110. &interface_to_usbdev(interface)->dev,
  2111. RFKILL_TYPE_WWAN,
  2112. &hso_rfkill_ops, hso_dev);
  2113. if (!hso_net->rfkill) {
  2114. dev_err(dev, "%s - Out of memory\n", __func__);
  2115. return;
  2116. }
  2117. if (rfkill_register(hso_net->rfkill) < 0) {
  2118. rfkill_destroy(hso_net->rfkill);
  2119. hso_net->rfkill = NULL;
  2120. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2121. return;
  2122. }
  2123. }
  2124. static struct device_type hso_type = {
  2125. .name = "wwan",
  2126. };
  2127. /* Creates our network device */
  2128. static struct hso_device *hso_create_net_device(struct usb_interface *interface,
  2129. int port_spec)
  2130. {
  2131. int result, i;
  2132. struct net_device *net;
  2133. struct hso_net *hso_net;
  2134. struct hso_device *hso_dev;
  2135. hso_dev = hso_create_device(interface, port_spec);
  2136. if (!hso_dev)
  2137. return NULL;
  2138. /* allocate our network device, then we can put in our private data */
  2139. /* call hso_net_init to do the basic initialization */
  2140. net = alloc_netdev(sizeof(struct hso_net), "hso%d", NET_NAME_UNKNOWN,
  2141. hso_net_init);
  2142. if (!net) {
  2143. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2144. goto exit;
  2145. }
  2146. hso_net = netdev_priv(net);
  2147. hso_dev->port_data.dev_net = hso_net;
  2148. hso_net->net = net;
  2149. hso_net->parent = hso_dev;
  2150. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2151. USB_DIR_IN);
  2152. if (!hso_net->in_endp) {
  2153. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2154. goto exit;
  2155. }
  2156. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2157. USB_DIR_OUT);
  2158. if (!hso_net->out_endp) {
  2159. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2160. goto exit;
  2161. }
  2162. SET_NETDEV_DEV(net, &interface->dev);
  2163. SET_NETDEV_DEVTYPE(net, &hso_type);
  2164. /* registering our net device */
  2165. result = register_netdev(net);
  2166. if (result) {
  2167. dev_err(&interface->dev, "Failed to register device\n");
  2168. goto exit;
  2169. }
  2170. /* start allocating */
  2171. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2172. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2173. if (!hso_net->mux_bulk_rx_urb_pool[i])
  2174. goto exit;
  2175. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2176. GFP_KERNEL);
  2177. if (!hso_net->mux_bulk_rx_buf_pool[i])
  2178. goto exit;
  2179. }
  2180. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2181. if (!hso_net->mux_bulk_tx_urb)
  2182. goto exit;
  2183. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2184. if (!hso_net->mux_bulk_tx_buf)
  2185. goto exit;
  2186. add_net_device(hso_dev);
  2187. hso_log_port(hso_dev);
  2188. hso_create_rfkill(hso_dev, interface);
  2189. return hso_dev;
  2190. exit:
  2191. hso_free_net_device(hso_dev);
  2192. return NULL;
  2193. }
  2194. static void hso_free_tiomget(struct hso_serial *serial)
  2195. {
  2196. struct hso_tiocmget *tiocmget;
  2197. if (!serial)
  2198. return;
  2199. tiocmget = serial->tiocmget;
  2200. if (tiocmget) {
  2201. usb_free_urb(tiocmget->urb);
  2202. tiocmget->urb = NULL;
  2203. serial->tiocmget = NULL;
  2204. kfree(tiocmget);
  2205. }
  2206. }
  2207. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2208. static void hso_free_serial_device(struct hso_device *hso_dev)
  2209. {
  2210. struct hso_serial *serial = dev2ser(hso_dev);
  2211. if (!serial)
  2212. return;
  2213. hso_serial_common_free(serial);
  2214. if (serial->shared_int) {
  2215. mutex_lock(&serial->shared_int->shared_int_lock);
  2216. if (--serial->shared_int->ref_count == 0)
  2217. hso_free_shared_int(serial->shared_int);
  2218. else
  2219. mutex_unlock(&serial->shared_int->shared_int_lock);
  2220. }
  2221. hso_free_tiomget(serial);
  2222. kfree(serial);
  2223. kfree(hso_dev);
  2224. }
  2225. /* Creates a bulk AT channel */
  2226. static struct hso_device *hso_create_bulk_serial_device(
  2227. struct usb_interface *interface, int port)
  2228. {
  2229. struct hso_device *hso_dev;
  2230. struct hso_serial *serial;
  2231. int num_urbs;
  2232. struct hso_tiocmget *tiocmget;
  2233. hso_dev = hso_create_device(interface, port);
  2234. if (!hso_dev)
  2235. return NULL;
  2236. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2237. if (!serial)
  2238. goto exit;
  2239. serial->parent = hso_dev;
  2240. hso_dev->port_data.dev_serial = serial;
  2241. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2242. num_urbs = 2;
  2243. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2244. GFP_KERNEL);
  2245. /* it isn't going to break our heart if serial->tiocmget
  2246. * allocation fails don't bother checking this.
  2247. */
  2248. if (serial->tiocmget) {
  2249. tiocmget = serial->tiocmget;
  2250. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2251. if (tiocmget->urb) {
  2252. mutex_init(&tiocmget->mutex);
  2253. init_waitqueue_head(&tiocmget->waitq);
  2254. tiocmget->endp = hso_get_ep(
  2255. interface,
  2256. USB_ENDPOINT_XFER_INT,
  2257. USB_DIR_IN);
  2258. } else
  2259. hso_free_tiomget(serial);
  2260. }
  2261. }
  2262. else
  2263. num_urbs = 1;
  2264. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2265. BULK_URB_TX_SIZE))
  2266. goto exit;
  2267. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2268. USB_DIR_IN);
  2269. if (!serial->in_endp) {
  2270. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2271. goto exit2;
  2272. }
  2273. if (!
  2274. (serial->out_endp =
  2275. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2276. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2277. goto exit2;
  2278. }
  2279. serial->write_data = hso_std_serial_write_data;
  2280. /* and record this serial */
  2281. set_serial_by_index(serial->minor, serial);
  2282. /* setup the proc dirs and files if needed */
  2283. hso_log_port(hso_dev);
  2284. /* done, return it */
  2285. return hso_dev;
  2286. exit2:
  2287. hso_serial_tty_unregister(serial);
  2288. hso_serial_common_free(serial);
  2289. exit:
  2290. hso_free_tiomget(serial);
  2291. kfree(serial);
  2292. kfree(hso_dev);
  2293. return NULL;
  2294. }
  2295. /* Creates a multiplexed AT channel */
  2296. static
  2297. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2298. int port,
  2299. struct hso_shared_int *mux)
  2300. {
  2301. struct hso_device *hso_dev;
  2302. struct hso_serial *serial;
  2303. int port_spec;
  2304. port_spec = HSO_INTF_MUX;
  2305. port_spec &= ~HSO_PORT_MASK;
  2306. port_spec |= hso_mux_to_port(port);
  2307. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2308. return NULL;
  2309. hso_dev = hso_create_device(interface, port_spec);
  2310. if (!hso_dev)
  2311. return NULL;
  2312. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2313. if (!serial)
  2314. goto exit;
  2315. hso_dev->port_data.dev_serial = serial;
  2316. serial->parent = hso_dev;
  2317. if (hso_serial_common_create
  2318. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2319. goto exit;
  2320. serial->tx_data_length--;
  2321. serial->write_data = hso_mux_serial_write_data;
  2322. serial->shared_int = mux;
  2323. mutex_lock(&serial->shared_int->shared_int_lock);
  2324. serial->shared_int->ref_count++;
  2325. mutex_unlock(&serial->shared_int->shared_int_lock);
  2326. /* and record this serial */
  2327. set_serial_by_index(serial->minor, serial);
  2328. /* setup the proc dirs and files if needed */
  2329. hso_log_port(hso_dev);
  2330. /* done, return it */
  2331. return hso_dev;
  2332. exit:
  2333. if (serial) {
  2334. tty_unregister_device(tty_drv, serial->minor);
  2335. kfree(serial);
  2336. }
  2337. kfree(hso_dev);
  2338. return NULL;
  2339. }
  2340. static void hso_free_shared_int(struct hso_shared_int *mux)
  2341. {
  2342. usb_free_urb(mux->shared_intr_urb);
  2343. kfree(mux->shared_intr_buf);
  2344. mutex_unlock(&mux->shared_int_lock);
  2345. kfree(mux);
  2346. }
  2347. static
  2348. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2349. {
  2350. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2351. if (!mux)
  2352. return NULL;
  2353. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2354. USB_DIR_IN);
  2355. if (!mux->intr_endp) {
  2356. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2357. goto exit;
  2358. }
  2359. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2360. if (!mux->shared_intr_urb)
  2361. goto exit;
  2362. mux->shared_intr_buf =
  2363. kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
  2364. GFP_KERNEL);
  2365. if (!mux->shared_intr_buf)
  2366. goto exit;
  2367. mutex_init(&mux->shared_int_lock);
  2368. return mux;
  2369. exit:
  2370. kfree(mux->shared_intr_buf);
  2371. usb_free_urb(mux->shared_intr_urb);
  2372. kfree(mux);
  2373. return NULL;
  2374. }
  2375. /* Gets the port spec for a certain interface */
  2376. static int hso_get_config_data(struct usb_interface *interface)
  2377. {
  2378. struct usb_device *usbdev = interface_to_usbdev(interface);
  2379. u8 *config_data = kmalloc(17, GFP_KERNEL);
  2380. u32 if_num = interface->cur_altsetting->desc.bInterfaceNumber;
  2381. s32 result;
  2382. if (!config_data)
  2383. return -ENOMEM;
  2384. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2385. 0x86, 0xC0, 0, 0, config_data, 17,
  2386. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2387. kfree(config_data);
  2388. return -EIO;
  2389. }
  2390. switch (config_data[if_num]) {
  2391. case 0x0:
  2392. result = 0;
  2393. break;
  2394. case 0x1:
  2395. result = HSO_PORT_DIAG;
  2396. break;
  2397. case 0x2:
  2398. result = HSO_PORT_GPS;
  2399. break;
  2400. case 0x3:
  2401. result = HSO_PORT_GPS_CONTROL;
  2402. break;
  2403. case 0x4:
  2404. result = HSO_PORT_APP;
  2405. break;
  2406. case 0x5:
  2407. result = HSO_PORT_APP2;
  2408. break;
  2409. case 0x6:
  2410. result = HSO_PORT_CONTROL;
  2411. break;
  2412. case 0x7:
  2413. result = HSO_PORT_NETWORK;
  2414. break;
  2415. case 0x8:
  2416. result = HSO_PORT_MODEM;
  2417. break;
  2418. case 0x9:
  2419. result = HSO_PORT_MSD;
  2420. break;
  2421. case 0xa:
  2422. result = HSO_PORT_PCSC;
  2423. break;
  2424. case 0xb:
  2425. result = HSO_PORT_VOICE;
  2426. break;
  2427. default:
  2428. result = 0;
  2429. }
  2430. if (result)
  2431. result |= HSO_INTF_BULK;
  2432. if (config_data[16] & 0x1)
  2433. result |= HSO_INFO_CRC_BUG;
  2434. kfree(config_data);
  2435. return result;
  2436. }
  2437. /* called once for each interface upon device insertion */
  2438. static int hso_probe(struct usb_interface *interface,
  2439. const struct usb_device_id *id)
  2440. {
  2441. int mux, i, if_num, port_spec;
  2442. unsigned char port_mask;
  2443. struct hso_device *hso_dev = NULL;
  2444. struct hso_shared_int *shared_int;
  2445. struct hso_device *tmp_dev = NULL;
  2446. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2447. dev_err(&interface->dev, "Not our interface\n");
  2448. return -ENODEV;
  2449. }
  2450. if_num = interface->cur_altsetting->desc.bInterfaceNumber;
  2451. /* Get the interface/port specification from either driver_info or from
  2452. * the device itself */
  2453. if (id->driver_info)
  2454. port_spec = ((u32 *)(id->driver_info))[if_num];
  2455. else
  2456. port_spec = hso_get_config_data(interface);
  2457. /* Check if we need to switch to alt interfaces prior to port
  2458. * configuration */
  2459. if (interface->num_altsetting > 1)
  2460. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2461. interface->needs_remote_wakeup = 1;
  2462. /* Allocate new hso device(s) */
  2463. switch (port_spec & HSO_INTF_MASK) {
  2464. case HSO_INTF_MUX:
  2465. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2466. /* Create the network device */
  2467. if (!disable_net) {
  2468. hso_dev = hso_create_net_device(interface,
  2469. port_spec);
  2470. if (!hso_dev)
  2471. goto exit;
  2472. tmp_dev = hso_dev;
  2473. }
  2474. }
  2475. if (hso_get_mux_ports(interface, &port_mask))
  2476. /* TODO: de-allocate everything */
  2477. goto exit;
  2478. shared_int = hso_create_shared_int(interface);
  2479. if (!shared_int)
  2480. goto exit;
  2481. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2482. if (port_mask & i) {
  2483. hso_dev = hso_create_mux_serial_device(
  2484. interface, i, shared_int);
  2485. if (!hso_dev)
  2486. goto exit;
  2487. }
  2488. }
  2489. if (tmp_dev)
  2490. hso_dev = tmp_dev;
  2491. break;
  2492. case HSO_INTF_BULK:
  2493. /* It's a regular bulk interface */
  2494. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2495. if (!disable_net)
  2496. hso_dev =
  2497. hso_create_net_device(interface, port_spec);
  2498. } else {
  2499. hso_dev =
  2500. hso_create_bulk_serial_device(interface, port_spec);
  2501. }
  2502. if (!hso_dev)
  2503. goto exit;
  2504. break;
  2505. default:
  2506. goto exit;
  2507. }
  2508. /* save our data pointer in this device */
  2509. usb_set_intfdata(interface, hso_dev);
  2510. /* done */
  2511. return 0;
  2512. exit:
  2513. hso_free_interface(interface);
  2514. return -ENODEV;
  2515. }
  2516. /* device removed, cleaning up */
  2517. static void hso_disconnect(struct usb_interface *interface)
  2518. {
  2519. hso_free_interface(interface);
  2520. /* remove reference of our private data */
  2521. usb_set_intfdata(interface, NULL);
  2522. }
  2523. static void async_get_intf(struct work_struct *data)
  2524. {
  2525. struct hso_device *hso_dev =
  2526. container_of(data, struct hso_device, async_get_intf);
  2527. usb_autopm_get_interface(hso_dev->interface);
  2528. }
  2529. static void async_put_intf(struct work_struct *data)
  2530. {
  2531. struct hso_device *hso_dev =
  2532. container_of(data, struct hso_device, async_put_intf);
  2533. usb_autopm_put_interface(hso_dev->interface);
  2534. }
  2535. static int hso_get_activity(struct hso_device *hso_dev)
  2536. {
  2537. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2538. if (!hso_dev->is_active) {
  2539. hso_dev->is_active = 1;
  2540. schedule_work(&hso_dev->async_get_intf);
  2541. }
  2542. }
  2543. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2544. return -EAGAIN;
  2545. usb_mark_last_busy(hso_dev->usb);
  2546. return 0;
  2547. }
  2548. static int hso_put_activity(struct hso_device *hso_dev)
  2549. {
  2550. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2551. if (hso_dev->is_active) {
  2552. hso_dev->is_active = 0;
  2553. schedule_work(&hso_dev->async_put_intf);
  2554. return -EAGAIN;
  2555. }
  2556. }
  2557. hso_dev->is_active = 0;
  2558. return 0;
  2559. }
  2560. /* called by kernel when we need to suspend device */
  2561. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2562. {
  2563. int i, result;
  2564. /* Stop all serial ports */
  2565. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2566. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2567. result = hso_stop_serial_device(serial_table[i]);
  2568. if (result)
  2569. goto out;
  2570. }
  2571. }
  2572. /* Stop all network ports */
  2573. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2574. if (network_table[i] &&
  2575. (network_table[i]->interface == iface)) {
  2576. result = hso_stop_net_device(network_table[i]);
  2577. if (result)
  2578. goto out;
  2579. }
  2580. }
  2581. out:
  2582. return 0;
  2583. }
  2584. /* called by kernel when we need to resume device */
  2585. static int hso_resume(struct usb_interface *iface)
  2586. {
  2587. int i, result = 0;
  2588. struct hso_net *hso_net;
  2589. /* Start all serial ports */
  2590. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2591. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2592. if (dev2ser(serial_table[i])->port.count) {
  2593. result =
  2594. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2595. hso_kick_transmit(dev2ser(serial_table[i]));
  2596. if (result)
  2597. goto out;
  2598. }
  2599. }
  2600. }
  2601. /* Start all network ports */
  2602. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2603. if (network_table[i] &&
  2604. (network_table[i]->interface == iface)) {
  2605. hso_net = dev2net(network_table[i]);
  2606. if (hso_net->flags & IFF_UP) {
  2607. /* First transmit any lingering data,
  2608. then restart the device. */
  2609. if (hso_net->skb_tx_buf) {
  2610. dev_dbg(&iface->dev,
  2611. "Transmitting"
  2612. " lingering data\n");
  2613. hso_net_start_xmit(hso_net->skb_tx_buf,
  2614. hso_net->net);
  2615. hso_net->skb_tx_buf = NULL;
  2616. }
  2617. result = hso_start_net_device(network_table[i]);
  2618. if (result)
  2619. goto out;
  2620. }
  2621. }
  2622. }
  2623. out:
  2624. return result;
  2625. }
  2626. static void hso_serial_ref_free(struct kref *ref)
  2627. {
  2628. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2629. hso_free_serial_device(hso_dev);
  2630. }
  2631. static void hso_free_interface(struct usb_interface *interface)
  2632. {
  2633. struct hso_serial *serial;
  2634. int i;
  2635. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2636. if (serial_table[i] &&
  2637. (serial_table[i]->interface == interface)) {
  2638. serial = dev2ser(serial_table[i]);
  2639. tty_port_tty_hangup(&serial->port, false);
  2640. mutex_lock(&serial->parent->mutex);
  2641. serial->parent->usb_gone = 1;
  2642. mutex_unlock(&serial->parent->mutex);
  2643. cancel_work_sync(&serial_table[i]->async_put_intf);
  2644. cancel_work_sync(&serial_table[i]->async_get_intf);
  2645. hso_serial_tty_unregister(serial);
  2646. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2647. set_serial_by_index(i, NULL);
  2648. }
  2649. }
  2650. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2651. if (network_table[i] &&
  2652. (network_table[i]->interface == interface)) {
  2653. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2654. /* hso_stop_net_device doesn't stop the net queue since
  2655. * traffic needs to start it again when suspended */
  2656. netif_stop_queue(dev2net(network_table[i])->net);
  2657. hso_stop_net_device(network_table[i]);
  2658. cancel_work_sync(&network_table[i]->async_put_intf);
  2659. cancel_work_sync(&network_table[i]->async_get_intf);
  2660. if (rfk) {
  2661. rfkill_unregister(rfk);
  2662. rfkill_destroy(rfk);
  2663. }
  2664. hso_free_net_device(network_table[i]);
  2665. }
  2666. }
  2667. }
  2668. /* Helper functions */
  2669. /* Get the endpoint ! */
  2670. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2671. int type, int dir)
  2672. {
  2673. int i;
  2674. struct usb_host_interface *iface = intf->cur_altsetting;
  2675. struct usb_endpoint_descriptor *endp;
  2676. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2677. endp = &iface->endpoint[i].desc;
  2678. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2679. (usb_endpoint_type(endp) == type))
  2680. return endp;
  2681. }
  2682. return NULL;
  2683. }
  2684. /* Get the byte that describes which ports are enabled */
  2685. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2686. {
  2687. int i;
  2688. struct usb_host_interface *iface = intf->cur_altsetting;
  2689. if (iface->extralen == 3) {
  2690. *ports = iface->extra[2];
  2691. return 0;
  2692. }
  2693. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2694. if (iface->endpoint[i].extralen == 3) {
  2695. *ports = iface->endpoint[i].extra[2];
  2696. return 0;
  2697. }
  2698. }
  2699. return -1;
  2700. }
  2701. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2702. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2703. struct usb_device *usb, gfp_t gfp)
  2704. {
  2705. int result;
  2706. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2707. usb_rcvintpipe(usb,
  2708. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2709. shared_int->shared_intr_buf,
  2710. 1,
  2711. intr_callback, shared_int,
  2712. shared_int->intr_endp->bInterval);
  2713. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2714. if (result)
  2715. dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
  2716. result);
  2717. return result;
  2718. }
  2719. /* operations setup of the serial interface */
  2720. static const struct tty_operations hso_serial_ops = {
  2721. .open = hso_serial_open,
  2722. .close = hso_serial_close,
  2723. .write = hso_serial_write,
  2724. .write_room = hso_serial_write_room,
  2725. .cleanup = hso_serial_cleanup,
  2726. .ioctl = hso_serial_ioctl,
  2727. .set_termios = hso_serial_set_termios,
  2728. .chars_in_buffer = hso_serial_chars_in_buffer,
  2729. .tiocmget = hso_serial_tiocmget,
  2730. .tiocmset = hso_serial_tiocmset,
  2731. .get_icount = hso_get_count,
  2732. .unthrottle = hso_unthrottle
  2733. };
  2734. static struct usb_driver hso_driver = {
  2735. .name = driver_name,
  2736. .probe = hso_probe,
  2737. .disconnect = hso_disconnect,
  2738. .id_table = hso_ids,
  2739. .suspend = hso_suspend,
  2740. .resume = hso_resume,
  2741. .reset_resume = hso_resume,
  2742. .supports_autosuspend = 1,
  2743. .disable_hub_initiated_lpm = 1,
  2744. };
  2745. static int __init hso_init(void)
  2746. {
  2747. int i;
  2748. int result;
  2749. /* put it in the log */
  2750. pr_info("%s\n", version);
  2751. /* Initialise the serial table semaphore and table */
  2752. spin_lock_init(&serial_table_lock);
  2753. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2754. serial_table[i] = NULL;
  2755. /* allocate our driver using the proper amount of supported minors */
  2756. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2757. if (!tty_drv)
  2758. return -ENOMEM;
  2759. /* fill in all needed values */
  2760. tty_drv->driver_name = driver_name;
  2761. tty_drv->name = tty_filename;
  2762. /* if major number is provided as parameter, use that one */
  2763. if (tty_major)
  2764. tty_drv->major = tty_major;
  2765. tty_drv->minor_start = 0;
  2766. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2767. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2768. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2769. tty_drv->init_termios = tty_std_termios;
  2770. hso_init_termios(&tty_drv->init_termios);
  2771. tty_set_operations(tty_drv, &hso_serial_ops);
  2772. /* register the tty driver */
  2773. result = tty_register_driver(tty_drv);
  2774. if (result) {
  2775. pr_err("%s - tty_register_driver failed(%d)\n",
  2776. __func__, result);
  2777. goto err_free_tty;
  2778. }
  2779. /* register this module as an usb driver */
  2780. result = usb_register(&hso_driver);
  2781. if (result) {
  2782. pr_err("Could not register hso driver - error: %d\n", result);
  2783. goto err_unreg_tty;
  2784. }
  2785. /* done */
  2786. return 0;
  2787. err_unreg_tty:
  2788. tty_unregister_driver(tty_drv);
  2789. err_free_tty:
  2790. put_tty_driver(tty_drv);
  2791. return result;
  2792. }
  2793. static void __exit hso_exit(void)
  2794. {
  2795. pr_info("unloaded\n");
  2796. tty_unregister_driver(tty_drv);
  2797. put_tty_driver(tty_drv);
  2798. /* deregister the usb driver */
  2799. usb_deregister(&hso_driver);
  2800. }
  2801. /* Module definitions */
  2802. module_init(hso_init);
  2803. module_exit(hso_exit);
  2804. MODULE_AUTHOR(MOD_AUTHOR);
  2805. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2806. MODULE_LICENSE(MOD_LICENSE);
  2807. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2808. MODULE_PARM_DESC(debug, "debug level mask [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2809. module_param(debug, int, S_IRUGO | S_IWUSR);
  2810. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2811. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2812. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2813. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2814. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2815. module_param(disable_net, int, S_IRUGO | S_IWUSR);