hso.c 83 KB

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