hso.c 75 KB

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